Android Question Screen capture Google map and views [SOLVED]

Roger Daley

Well-Known Member
Licensed User
Longtime User
Hi All,

I am trying to capture and save the screen containing a map and views. The attached code is succesful in getting the screenshot of views and the screenshot of the Google map in two different BitMaps. The trick is to overlay these two BitMaps as you would with layers in PhotoShop [Et Al]

I have tried to follow a post by Inman but failed:

You can do all this with Canvas object. Here is how.
  1. Save snapshot of the map using Extras library
  2. Take the snapshot of Panel using Canvas object
  3. Define a Rect with borders corresponding to the MapView
  4. Convert snapshot from step 1 to BitmapDrawable
  5. Using Canvas from step 2, perform Canvas.DrawDrawable with BitmapDrawable from step 5 to the Rect defined in step 3
  6. Save the Bitmap from Canvas with Canvas.Bitmap.WritetoStream
  7. Done

Undoubtably my short coming not Inmans.


Below is the code that I have tried in many variations producing a variety of errors.

B4X:
Sub BtnSave_Click

    Vibrate.Vibrate (75)         ' Vibrate phone for 75 ms  === Phone Library
    Private b As Beeper                ' Audio Library
    b.Initialize(50, 600)         ' 50 milliseconds, 600 hz
    b.Beep
   
    Private Obj1, Obj2 As Reflector
    Private bmp As Bitmap
    Private c As Canvas
    Private Out As OutputStream
    Private args(1) As Object
    Private types(1) As String
'    Private InMap As Object
'    Private In As InputStream
'    Private BmpMap As Bitmap
'    Private w As Int 'width
'    Private l As Int 'length
'    Private BmpMutable As Bitmap 
'    Private MapCanvas As Canvas    
'    Private DestRect As Rect
    Private Out As OutputStream
   

' Take a screenshot.   
    Obj1.Target = Obj1.GetActivityBA
    Obj1.Target = Obj1.GetField("vg")
    bmp.InitializeMutable(Activity.Width, Activity.Height)
    c.Initialize2(bmp)
    Obj2.Target = c
    Obj2.Target = Obj2.GetField("canvas")
    args(0) = Obj2.Target
    types(0) = "android.graphics.Canvas"
    Obj1.RunMethod4("draw", args, types)
   
'Save ScreenShot as ScrnShotTemp.png
    If File.Exists(File.DirDefaultExternal&"/Download/", "ScrnShotTemp.png") Then
        File.Delete(File.DirDefaultExternal&"/Download/", "ScrnShotTemp.png")
    End If   
    Out = File.OpenOutput(File.DirRootExternal&"/Download/", "ScrnShotTemp.png", False)
    bmp.WriteToStream(Out, 100, "PNG")
    Out.Close
   
'Take ScreenShot of Google map, save as map.png
    MapEx.Snapshot(gmap, "gmap")





'Save as MapMutable.png
'    In =  File.OpenInput(File.DirRootExternal&"/Download/", "map.png")         'Map.png
'    InMap = In
'    BmpMap = InMap
'    'Create a mutable Bitmap with Bitmap.InitializeMutable.
'    w = BmpMap.Width
'    l = BmpMap.Height  
'    BmpMutable.InitializeMutable(w,l) ' get layout from device layout ?? or from bg ?
'    'Create a Canvas For this Bitmap.
'    MapCanvas.Initialize(Activity)
'    'Draw the drawable with Canvas.DrawDrawable
'    DestRect.Initialize(0,0, w, l)
'    MapCanvas.DrawDrawable(BmpMapD,DestRect)
'    'Save the Bitmap To a File (Bitmap.WriteToStream)
'    Out = File.OpenOutput(File.DirRootExternal, "MapMutable.png", False)
'    BmpMutable.WriteToStream(Out, 100, "PNG")
  



    ' Input file name
       If FileFlag = 0 Then            'FileFlag is set to 1 by InPutFileName Sub if valid file name.
           InPutFileName
        Return                        'Ignore
    End If
  
    'Save using FileName
    Out = File.OpenOutput(File.DirRootExternal&"/Download/", FileName, False)
    bmp.WriteToStream(Out, 100, "PNG")
    Out.Close
   
    'Reset
    FileFlag = 0
    FileName = ""
    DispInput.Text = ""
End Sub

Sub gmap_SnapshotReady (Bitmap1 As Bitmap)
    If File.Exists(File.DirRootExternal&"/Download/", "map.png") Then File.Delete(File.DirRootExternal&"/Download/", "map.png")
    Private out As OutputStream
    out = File.OpenOutput(File.DirRootExternal&"/Download/", "map.png", False)
    Bitmap1.WriteToStream(out, 100, "PNG")
    out.Close
End Sub

The question comes down to:
How do I merge two BitMaps in to one BitMap?

Thanks in advance.

Regards
Roger
 
Top