Android Question how to save a imageview.background to file?

yhyzhj

Member
I want to know how to save a imageview.bakcground to file.
And I want to know how to save a icon of app to file.
pleae help me!
 

kisoft

Well-Known Member
Licensed User
Longtime User
Write exactly what you want to achieve and what you need such a solution for ... Maybe it can be solved in a different way ...
 
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
Try this, you can also use GetSafeDirDefaultExternal if you really want to...
 

Attachments

  • ImageView.zip
    18.1 KB · Views: 154
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
better to use B4XImageView multi platform

B4X:
Sub Class_Globals
    Private xui As XUI
    Private B4XImageView1 As B4XImageView
End Sub

B4X:
    'Load Image
    B4XImageView1.Bitmap = xui.LoadBitmap(File.DirAssets, "img.jpg")
    B4XImageView1.ResizeMode = "NONE" 'or "FILL"
    B4XImageView1.Update
      
    'Save to file
    SaveBitmapToFile(B4XImageView1.Bitmap, xui.DefaultFolder, "ImageView1.png")

B4X:
Public Sub SaveBitmapToFile(Image As B4XBitmap, DirFolder As String, FileName As String)
    Dim Out As OutputStream
    Out = File.OpenOutput(DirFolder, FileName, False)
    Image.WriteToStream(Out, 100, "PNG")
    Out.Close
End Sub
 
Last edited:
Upvote 0

yhyzhj

Member
or ImageView
B4X:
icon=pm.GetApplicationIcon(pkgn(i))
B4X:
ImageView1.Background=icon
B4X:
SaveBitmapToFile(ImageView1.Background,xui.DefaultFolder,"iv1.png")

show Error description: Undeclared variable 'savebitmaptofile' is used before it was assigned any value.
 
Upvote 0

yhyzhj

Member
Write exactly what you want to achieve and what you need such a solution for ... Maybe it can be solved in a different way ...
i just want to write a launcher app. i have got all icon of apps, now need put the icons into gridview. but I use GridView.AddImage("",icon.Bitmap), show dir can not be null. so, i think that must save the icons to files. then i can use GridView.AddImage(dir,imagefile).
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
sample with BitmapDrawable and icon APP

you APP Application.PackageName

B4X:
    Dim pm As PackageManager
    Dim icon As BitmapDrawable = pm.GetApplicationIcon(Application.PackageName)
    ImageView1.SetBackgroundImage(icon.Bitmap)
    SaveBitmapToFile(ImageView1.Bitmap, xui.DefaultFolder, "myicon.png")

or

B4X:
    Dim pm As PackageManager
    Dim icon As BitmapDrawable = pm.GetApplicationIcon(Application.PackageName)
    ImageView1.Background = icon
    SaveBitmapToFile(ImageView1.Bitmap, xui.DefaultFolder, "myicon.png")
 
Last edited:
Upvote 0

yhyzhj

Member
sample with BitmapDrawable and icon APP

you APP Application.PackageName

B4X:
    Dim pm As PackageManager
    Dim icon As BitmapDrawable = pm.GetApplicationIcon(Application.PackageName)
    ImageView1.SetBackgroundImage(icon.Bitmap)
    SaveBitmapToFile(ImageView1.Bitmap, xui.DefaultFolder, "myicon.png")

or

B4X:
    Dim pm As PackageManager
    Dim icon As BitmapDrawable = pm.GetApplicationIcon(Application.PackageName)
    ImageView1.Background = icon
    SaveBitmapToFile(ImageView1.Bitmap, xui.DefaultFolder, "myicon.png")

thank you! it works!
if I want to add icons into GridView, how to do?
 
Upvote 0
Top