Android Question resize image before saving

3394509365

Active Member
Licensed User
Longtime User
hello, i need to upload a photo from phone memory into an imageview.

At this point I want to save it in a new folder only if it has the allowed size.

How can I do, in case to do the resizing of the image contained in the imageview?
 

Sagenut

Expert
Licensed User
Longtime User
B4X:
Dim iv As ImageView                                                               'Create a new ImageView (if not by Designer)
iv.Initialize("")                                                                 'Initialize the View (if not by Designer)
iv.Bitmap = LoadBitmap(File.DirAssets, "Immagine.png")                            'Load a Bitmap to Imageview (use your path and filename)
Dim bmp As Bitmap = iv.Bitmap                                                     'Create a Bitmap and set it to the image loaded into Imageview
bmp = bmp.Resize((bmp.Width / 5), (bmp.Height / 5), True)                         'Resize the bitmap to 1/5 of the initial size
Dim Out As OutputStream                                                           'Create an output stream
Out = File.OpenOutput(rp.GetSafeDirDefaultExternal(""), "Test.png", False)        'Set path and filename for the new file
bmp.WriteToStream(Out, 100, "PNG")                                                'Write the new file with resized bitmap
Out.Close                                                                         'Close the stream job
iv.SetLayout(0, 0, bmp.Width, bmp.Height)                                         'Set the Imageview to the resized bitmap size
iv.Bitmap = LoadBitmap(rp.GetSafeDirDefaultExternal(""), "Test.png")              'Load the new resized bitmap from storage (use your path and filename)
iv.Gravity = Gravity.FILL                                                         'Set the Gravity to fill the Image View
Activity.AddView(iv, 0, 0, iv.Width, iv.Height)                                   'Show the Imageview
The ImageView creation and initialization must be avoided if it was created in the Layout by Designer.
 
Last edited:
Upvote 0

3394509365

Active Member
Licensed User
Longtime User
I am integrating your code and it seems to work, but if I want to know how much the new created photo weighs how do I do it?
Is the filexxx.size or filexxx.lenght statement missing in V4A?

Thanks
 
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
B4X:
File.Size(path, filename)
It return the size in Bytes.
The result is a Long.
 
Upvote 0
Top