Android Question Save Blob image from remote Databse to Temporary file on the device

Anser

Well-Known Member
Licensed User
Longtime User
Hi,

I have an activity that downloads 70 to 100 images from a remote MySQL database (via jRDC) and displays on the picture on an ImageView placed on a CustomListView. The size of each image would be 120 KiB to 150 KiB. The resolution would be 640 x 289.

I understand that I may get Out of Memory (Unfortunately the app has stopped) error and that it will be depended on the device's free memory. So to avoid that, Erel had advised to download the Image data from the remote database and then write to a temporary file and then to read the temporary file and to use LoadBitmapSample to display the image.

As I haven't done anything like this, I have many doubts. I want a confirmation that I am doing it in the right way.

I will be using a For Next loop to process each records. So for each record in the for next loop, once I save the image data of each record to the temporary file, I will be using the following code

B4X:
'This is to read image Data from Table and convert it to a Bitmap and the Bitmap will be displayed on the ImageView
Dim Buffer() As Byte
Buffer = records(result.Columns.Get("Picture"))
'Need to find out a way to write this Byte array data as a Temporary file on File.DirRootExternal

Dim ImgViewLogo As ImageView
ImgViewLogo.Initialize("")

Dim BmpImg As Bitmap
'The following line OR
BmpImg.Initialize(File.DirRootExternal, "MyTempFile.png")   
'OR
BmpImg=LoadBitMapSample(File.DirRootExternal, "MyTempFile.png", ImgView.Width, ImgView.Width)
  
ImgViewLogo.Bitmap = BmpImg
ImgViewLogo.Gravity = Gravity.FILL

So after the code
BmpImg.Initialize(File.DirRootExternal, "MyTempFile01.png")
OR
LoadBitMapSample
Can I delete this Temporary file within the For Next loop from the File.DirRootExternal ?
If not then I need to create Unique file name for each temporary image file

Do every device have File.DirRootExternal with permission to write? Do I need to check this ?

Can anyone point me to the right direction, a sample code ?

Regards
Anser
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Dim buffer() As Byte = records(result.Columns.Get("Picture"))
Dim out As OutputStream = File.OpenOutput(File.DirInternal, "temp.png"))
out.WriteBytes(buffer, 0, buffer.Length)
out.Close
...
ImgViewLogo.Bitmap = LoadBitmapSample(File.DirInternal, "temp.png", ...)

Do every device have File.DirRootExternal with permission to write? Do I need to check this ?
You need to check it or just use File.DirInternal.

Can I delete this Temporary file within the For Next loop from the File.DirRootExternal ?
Yes. You can overwrite the same file in the for loop.
 
Upvote 0
Top