converting drawable object to a bitmap file

Erel

B4X founder
Staff member
Licensed User
Longtime User
1. Create a mutable bitmap with Bitmap.InitializeMutable.
2. Create a canvas for this bitmap.
3. Draw the drawable with Canvas.DrawDrawable
4. Save the bitmap to a file (Bitmap.WriteToStream)

In some cases the drawable might actually be a BitmapDrawable.
In that case it is simpler:
B4X:
If d Is BitmapDrawable Then
 Dim bd = d
 bd.Bitmap.WriteToStream(...)
End If
 
Upvote 0

melamoud

Active Member
Licensed User
Longtime User
write to stream ?

I'm doing:
B4X:
Dim bg As Object
    bg = GetWallpaperBackground
   'Activity.Background = bg
   If bg Is BitmapDrawable Then
      Dim bd As BitmapDrawable
       bd = bg
      Dim Out As OutputStream
      Out = File.OpenOutput(File.DirRootExternal, "saved_wallpaper.png", False)
      bd.Bitmap.WriteToStream(Out, 100, "PNG")
      Out.Close
   End If

I get error in compilation that writeToStream is not a member method of bitmap
 
Upvote 0

melamoud

Active Member
Licensed User
Longtime User
ok this is compiling .. I will try to see if it worked
B4X:
Dim bg As Object
    bg = GetWallpaperBackground
   'Activity.Background = bg
   If bg Is BitmapDrawable Then
      Dim bd As BitmapDrawable
       bd = bg
      Dim Out As OutputStream
      Out = File.OpenOutput(File.DirRootExternal, "saved_wallpaper.png", False)
      'bd.WriteToStream(Out, 100, "PNG")
      Dim btm As Bitmap
      btm = bd.Bitmap
      btm.WriteToStream(Out, 100, "PNG")
      Out.Close

I do not understand why the internal bitmap did not worked, I had to cast it
can you explain ?
 
Upvote 0

melamoud

Active Member
Licensed User
Longtime User
I think this will work, I will try it

B4X:
Dim bg As Object
    bg = GetWallpaperBackground
   'Activity.Background = bg
   If bg Is BitmapDrawable Then
      ToastMessageShow("is drawable!",True)
      Dim bd As BitmapDrawable
       bd = bg
      Dim Out As OutputStream
      Out = File.OpenOutput(File.DirRootExternal, "saved_wallpaper.png", False)
      'bd.WriteToStream(Out, 100, "PNG")
      Dim btm As Bitmap
      btm = bd.Bitmap
      btm.WriteToStream(Out, 100, "PNG")
      Out.Close
   Else
      ToastMessageShow("is NOT drawable!",True)
      Dim bt As Bitmap
      bt = bg
      'Create a mutable Bitmap with Bitmap.InitializeMutable.
      Dim w As Int 'width
      Dim l As Int 'length
      w = bt.Width
      l = bt.Height            
      Dim btm As Bitmap         
      btm.InitializeMutable(w,l) ' todo get layout from device layout ?? or from bg ?
      'Create a Canvas For this Bitmap.
      Dim can As Canvas
      'Draw the drawable with Canvas.DrawDrawable
      Dim DestRect As Rect
      DestRect.Initialize(0,0, w, l)
      can.DrawDrawable(bg,DestRect)
      'Save the Bitmap To a File (Bitmap.WriteToStream)
      Dim Out As OutputStream
      Out = File.OpenOutput(File.DirRootExternal, "saved_wallpaper1.png", False)
      btm.WriteToStream(Out, 100, "PNG")
   End If
 
Upvote 0
Top