Android Example Take a Screenshot with Inline Java Code

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:
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

  • b4aTakeScreenshot.zip
    16.3 KB · Views: 1,021

Star-Dust

Expert
Licensed User
Longtime User
Sorry, I'm OT.... but i want known...
ViewGroup is a Panel of B4A?
 

Star-Dust

Expert
Licensed User
Longtime 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
 

Johan Schoeman

Expert
Licensed User
Longtime User

Almora

Well-Known Member
Licensed User
Longtime User
I did not test but can the video capture the screen image?
 

Erel

B4X founder
Staff member
Licensed User
Longtime 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:

Johan Schoeman

Expert
Licensed User
Longtime User
decorChild.RunMethod("setDrawingCacheEnabled", Array(False)) was missing from my previous code. Fixed.
Thanks @Erel. Have tested both solutions on a KitKat device and on KitKat also working 100%.
 
Top