Android Example Take a Screenshot with Inline Java Code

Discussion in 'Tutorials & Examples' started by Johan Schoeman, Dec 2, 2017.

  1. Johan Schoeman

    Johan Schoeman Expert Licensed User

    I have tested this on an Android 7.0 device only - take a screenshot of your activity. You need to enable the JavaObject library to make use of it.

    @JordiCP solved my initial problem here (thanks @JordiCP):
    https://www.b4x.com/android/forum/t...tivity-to-inline-java-code.86796/#post-549360

    You need to handle the returned bitmap in your B4A code (i.e saving it, etc, etc, .....)

    Sample Code:
    Code:
    #Region  Project Attributes
        
    #ApplicationLabel:b4aTakeScreenshot
        
    #VersionCode: 1
        
    #VersionName:
        
    'SupportedOrientations possible values: unspecified, landscape or portrait.
        #SupportedOrientations: unspecified
        
    #CanInstallToExternalStorage: False
    #End Region

    #Region  Activity Attributes
        
    #FullScreen: False
        
    #IncludeTitle: True
    #End Region

    Sub Process_Globals
        
    'These global variables will be declared once when the application starts.
        'These variables can be accessed from all modules.

    End Sub

    Sub Globals
        
    'These global variables will be redeclared each time the activity is created.
        'These variables can only be accessed from this module.
        Dim nativeMe As JavaObject

        
    Private Button1 As Button
        
    Private iv1 As ImageView
        
    Private bm As Bitmap
       
    End Sub

    Sub Activity_Create(FirstTime As Boolean)
        
    'Do not forget to load the layout file created with the visual designer. For example:
        Activity.LoadLayout("main")
        nativeMe.InitializeContext
        bm.Initialize(
    File.DirAssets,"a.png")
        iv1.Bitmap = bm

    End Sub

    Sub Activity_Resume

    End Sub

    Sub Activity_Pause (UserClosed As Boolean)

    End Sub

    Sub Button1_Click
       
        bm = nativeMe.RunMethod(
    "takeScreenshot"Array(nativeMe)) 'YOU NEED TO HANDLE THE BITMAP FROM HERE ONWARDS.....
        iv1.Bitmap = bm
           
    End Sub

    #If Java

    import android.app.Activity;
    import android.graphics.Bitmap;
    import android.view.ViewGroup;

    public static Bitmap takeScreenshot(Activity activity) {
            ViewGroup decor = (ViewGroup) activity.getWindow().getDecorView();
            ViewGroup decorChild = (ViewGroup) decor.getChildAt(0);
            decorChild.setDrawingCacheEnabled(true);
            decorChild.buildDrawingCache();
            Bitmap drawingCache = decorChild.getDrawingCache(true);
            Bitmap bitmap = Bitmap.createBitmap(drawingCache);
            decorChild.setDrawingCacheEnabled(false);
            return bitmap;
    }

    #End If
     

    Attached Files:

    mmieher, MarcoRome, Erel and 3 others like this.
  2. Star-Dust

    Star-Dust Expert Licensed User

    Sorry, I'm OT.... but i want known...
    ViewGroup is a Panel of B4A?
     
  3. Johan Schoeman

    Johan Schoeman Expert Licensed User

    There is no panel added via B4A.
     
  4. Star-Dust

    Star-Dust Expert Licensed User

    Yes, I know it's not a panel added by B4A. I expressed myself badly, I do not write English well. But maybe it's better that I open a Topic on purpose, otherwise you'll have the topics too off topic

    I'm sorry
     
  5. Johan Schoeman

    Johan Schoeman Expert Licensed User

    The Java code comes from here...

    https://gist.github.com/PDDStudio/36383f6fecf815c7ee7a
     
    Star-Dust likes this.
  6. Almora

    Almora Active Member Licensed User

    I did not test but can the video capture the screen image?
     
  7. Johan Schoeman

    Johan Schoeman Expert Licensed User

    No - juts a screen shot
     
    Almora likes this.
  8. Erel

    Erel Administrator Staff Member Licensed User

    Same implementation based on JavaObject:
    Code:
    Sub TakeScreenshot As Bitmap
       
    Dim jo As JavaObject
       jo.InitializeContext
       
    Dim decor As JavaObject = jo.RunMethodJO("getWindow"Null).RunMethod("getDecorView"Null)
       
    Dim decorChild As JavaObject = decor.RunMethod("getChildAt"Array(0))
       decorChild.RunMethod(
    "setDrawingCacheEnabled"Array(True))
       decorChild.RunMethod(
    "buildDrawingCache"Null)
       
    Dim bmp As Bitmap = decorChild.RunMethod("getDrawingCache"Array(True))
       bmp.Initialize3(bmp)
       decorChild.RunMethod(
    "setDrawingCacheEnabled"Array(False))
       
    Return bmp
    End Sub
     
    Last edited: Dec 3, 2017
  9. Erel

    Erel Administrator Staff Member Licensed User

    decorChild.RunMethod("setDrawingCacheEnabled", Array(False)) was missing from my previous code. Fixed.
     
    Johan Schoeman likes this.
  10. Johan Schoeman

    Johan Schoeman Expert Licensed User

    Thanks @Erel. Have tested both solutions on a KitKat device and on KitKat also working 100%.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice