Android Question How? Capture screen from Starter service.

Lee Gillie CCP

Active Member
Licensed User
Longtime User
I have been using this to capture screen to a bitmap...

B4X:
Public Sub CaptureScreen( act As Activity, dir As String, fileName As String)
    Dim Obj1, Obj2 As Reflector
    Dim bmp As Bitmap
    Dim c As Canvas
    Obj1.Target = Obj1.GetActivityBA
    Obj1.Target = Obj1.GetField("vg")
    bmp.InitializeMutable(act.Width, act.Height)
    c.Initialize2(bmp)
    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( dir, fileName, False)
    Dim fmt As String = "PNG"
    If fileName.ToUpperCase.Contains("JPG") Then fmt = "JPEG"
    If fileName.ToUpperCase.Contains("JPEG") Then fmt = "JPEG"
    bmp.WriteToStream(Out, 100, fmt)
    Out.Close
End Sub

But I am NOW trying to move this to a Starter service module, to get a screen bitmap as part of an error report for the new global exception handler capability. Previously this was invoked from an Activity (by calling from various exception handlers). But in its new home, I don't see how to initialize the bitmap, nor to pass to android.graphics.Canvas.draw.

Is there some approach to get a bitmap of the entire device screen from a service?
 

Lee Gillie CCP

Active Member
Licensed User
Longtime User
Ok, I've researched the question for Android in general. It is a security feature of Android. But it looks like there are 3rd party services you can install for this, which do not require root. No idea how these work, nor why the same approach couldn't be done within my app. But I'll chalk that up to one of the deep mysteries of Android.

I see your idea, and I take from that...
If I have this routine coded into each and every activity....
And I track "current activity" in a global, hooking the update from various activity events...
Then, the starter service COULD invoke a screen capture in Application_Error.

I had thought about this, but wasn't sure what the state of the activity would be, or if a method could be called from the starter service from the context of the Application_Error routine. I am inferring from your reply that it should work.
 
Upvote 0
Top