Android Example Take a Screenshot with Inline Java Code

Similar threads

B4A Tutorial Inline Java Code
B4A Question Screenshot of Camera preview
B4A Question snapshot to camera and UI
B4A Question Share App-View
B4A Question Crash Report??

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/threads/passing-b4a-activity-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:
B4X:
#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
 

Attachments

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
 

Erel

Administrator
Staff member
Licensed User
Same implementation based on JavaObject:
B4X:
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:

Erel

Administrator
Staff member
Licensed User
decorChild.RunMethod("setDrawingCacheEnabled", Array(False)) was missing from my previous code. Fixed.
 
Top