Take Screenshot in B4A version 2.0


Active Member
Licensed User
The take screenshot is working in the B4A versions prior to v2.0. However, when I tried it in b4a v2.0. The application crashed with an error.
Sub TakeScreenshot (Activity As Activity)
    Log("TakeScreenshot activated")
       ' Take a screenshot.
       Dim Obj1, Obj2 As Reflector
       Dim bmp As Bitmap
       Dim c As Canvas
       Dim now, i As Long
       Dim dt As String
       DateTime.DateFormat = "yyMMddHHmmss"
       now = DateTime.now
       dt = DateTime.date(now) ' e.g.: "110812150355" is Aug.12, 2011, 3:03:55 p.m.
       Obj1.Target = Obj1.GetActivityBA
       Obj1.Target = Obj1.GetField("vg")
      bmp.InitializeMutable(Activity.Width, Activity.Height)
       Dim args(1) As Object
       Dim types(1) As String
       Obj2.Target = c
       Obj2.Target = Obj2.GetField("canvas")
       args(0) = Obj2.Target
       types(0) = "android.graphics.Canvas"
       Obj1.RunMethod4("draw", args, types)
       Dim Out As OutputStream
       Out = File.OpenOutput(File.DirRootExternal, dt & ".png", False)
       bmp.WriteToStream(Out, 100, "PNG")
    ToastMessageShow("Screenshot Captured", False)
End Sub
and here is the error log
TakeScreenshot activated
functions_takescreenshot (B4A line: 2047)
Obj1.Target = Obj1.GetActivityBA
java.lang.NoSuchFieldError: anywheresoftware.b4a.BA.activityBA
    at anywheresoftware.b4a.agraham.reflection.Reflection.GetActivityBA(Reflection.java:409)
    at com.home.cafiq.test.functions._takescreenshot(functions.java:766)
    at com.home.cafiq.test.search._activity_longclick(search.java:462)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:170)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:158)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:154)
    at anywheresoftware.b4a.objects.ViewWrapper$2.onLongClick(ViewWrapper.java:64)
    at android.view.View.performLongClick(View.java:3547)
    at android.view.View$CheckForLongPress.run(View.java:14087)
    at android.os.Handler.handleCallback(Handler.java:605)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4424)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
    at dalvik.system.NativeStart.main(Native Method)


Licensed User
I can use this feature to capture other assets of the screen?
or it is linked only with the current task?



Active Member
Licensed User
When I use this code, the resulting image turns out to have a transparent background instead of the normal black.