Android Question How to make a screenshot and store in the gallery

rwblinn

Well-Known Member
Licensed User
Longtime User
Hi,

seeking for advice on how to make a screenshot (via an image button) of the actual screen and store as a picture in the gallery (to then f.e. send via Whats App).

Example Screen from my roTrackX App.
upload_2016-1-20_15-35-21.png
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

rwblinn

Well-Known Member
Licensed User
Longtime User
Thanks for the advice.
Have taken the code from the beginners guide section 18.15 to make a screenshot of a GoogleMap.
Changed
B4X:
Obj1.Target = Obj1.GetField("vg")
to
B4X:
Obj1.Target = pnlMap
where the pnlMap is a Panel in which a MapFragment is parented to via
B4X:
MapFragment.Initialize("Map", pnlMap)

The result is a black image with bottom left the Google Image displayed. Instead of pnlMap tried pnlMap.GetView(0), but same result.
Q: How to get the GoogleMap image with the WayPoints and the Markers as shown in Post #1?

Explored further and found here :
According Android Google Map API V2 OpenGL layer, an interface has been implemented onSnapshotReady (Bitmap snapshot).
Q: Would it be possible to use in B4A?
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can use this code:
B4X:
Sub Map1_Click (Point As LatLng)
   Snapshot
End Sub

Sub Snapshot
   Dim jo As JavaObject = gmap
   Dim event As JavaObject = jo.CreateEvent("com.google.android.gms.maps.GoogleMap$SnapshotReadyCallback", "snapshot", Null)
   jo.RunMethod("snapshot", Array(event))
End Sub

Sub snapshot_Event (MethodName As String, Args() As Object) As Object
   Log("snapshot_event")
   If Args.Length > 0 Then
     Dim bmp As Bitmap = Args(0)
   End If
   Return Null
End Sub

Quoting Google's documentation:

Note: Images of the map must not be transmitted to your servers, or otherwise used outside of the application. If you need to send a map to another application or user, send data that allows them to reconstruct the map for the new user instead of a snapshot.
 
Upvote 0

rwblinn

Well-Known Member
Licensed User
Longtime User
For those interested, the code completed for saving and in addition sending the screenshot bitmap to the gallery. Tested under Android 5.1 and 4.4+
B4X:
Sub snapshot_Event (MethodName As String, Args() As Object) As Object
    If Args.Length > 0 Then
        Dim Filename As String = "rotrackx-track.png"
        Dim Folder As String = File.DirInternal
        Dim bmp As Bitmap = Args(0)
        Dim Out As OutputStream
        'Write the image to a file
        Try
            If File.Exists(Folder, Filename) Then File.Delete(Folder, Filename)
            Out = File.OpenOutput(Folder, Filename, False)
            bmp.WriteToStream(Out, 100, "PNG")
            Out.Close
            ToastMessageShow($"Screenshot saved to ${Filename}"$, False)
        Catch
            Msgbox2($"Error: Screenshot can not be saved.${CRLF}${LastException.Message}"$, "Save Track", "OK", "", "", Null)
            Return Null
        End Try
        'Send the bitmap to the Gallery
        Try
            Dim r As Reflector
            r.Target = r.GetContext
            r.Target = r.RunMethod("getContentResolver")
            Dim mediaStore As JavaObject
            mediaStore.InitializeStatic("android.provider.MediaStore.Images.Media")
            Dim Url As String = mediaStore.RunMethod("insertImage", Array As Object(r.Target, bmp, "roTrackX", "Track Screenshot"))
            ToastMessageShow($"Screenshot send to the Gallery."$, False)
        Catch
            Msgbox2($"Error: Screenshot can not be send to the gallery.${CRLF}${LastException.Message}"$, "Save Track", "OK", "", "", Null)
            Return Null
        End Try
   End If
   Return Null
End Sub
 
Upvote 0
Top