Android Question Load bitmap without block UI

Emme Developer

Well-Known Member
Licensed User
Longtime User
As i understood, resumable subs are running in the main thread. I have a small procject with a tablayout. In each tab (3) i have a scroll view with some imageviews, that download a thumbnail from a server and load the bitmap. For achieve this, i have a single routine that load the image. Each image calls a sub to download the image, located in starter service, and wait the download. When i try to download, the device will become a bit unresponsive. Is there a way to load the bitmaps meanwhile they are downloaded, without blocking UI?

Thanks, sorry for my english

B4X:
'Place image view sub
Sub loadScv
....
ListOfFiles = parser.NextArray
    For Each v As String In ListOfFiles
        Dim itm As Item = Utils.GetItemFromString(v)
        AddNewItem(itm)
        lstFile.Add(itm)
        Sleep(0)
    Next
End sub

B4X:
Sub AddNewItem(itm As Item)
'....
'.. add panel, imageview and other on scv....
'....
    If File.Exists(Utils.DirThumb,itm.name&extension) Then
        FillImageToView(LoadBitmap(Utils.DirThumb,itm.name&extension),im)
    Else
        Wait For (CallSub2(Starter,"Download",Utils.UrlThumbnail&"/"&itm.name&extension)) Complete(j As HttpJob)
        If j.Success  Then
            FillImageToView(j.GetBitmap,im)
            Dim out As OutputStream = File.OpenOutput(Utils.DirThumb,itm.name&extension,False)
            j.GetBitmap.WriteToStream(out,100,"PNG")
            out.Close
        End If
    End If
    Sleep(0)
   
End Sub


B4X:
'Starter service
Sub Download(url As String) As ResumableSub
    Dim j As HttpJob
    j.Initialize("",Me)
    j.Download(url)
    wait for (j) JobDone(j As HttpJob)
    Return j
End Sub
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
The downloading step always happen in the background.

1. Calling Job.GetBitmap.WriteToStream is very not efficient. You are first loading the bitmap and then save it.

Replace it with File.Copy2(j.GetInputStream, out)

2. Never call Job.GetBitmap unless you are sure that the images are small. Use Job.GetBitmapResize instead.
3. Your current code downloads all images at once. The Sleep(0) in the For Each loop doesn't do anything useful. If you want to download them one by one then you should wait for AddNewItem to complete.
 
Upvote 0

Emme Developer

Well-Known Member
Licensed User
Longtime User
The downloading step always happen in the background.

1. Calling Job.GetBitmap.WriteToStream is very not efficient. You are first loading the bitmap and then save it.

Replace it with File.Copy2(j.GetInputStream, out)

2. Never call Job.GetBitmap unless you are sure that the images are small. Use Job.GetBitmapResize instead.
3. Your current code downloads all images at once. The Sleep(0) in the For Each loop doesn't do anything useful. If you want to download them one by one then you should wait for AddNewItem to complete.
1) So should I first load the bitmap (resized) and after use file.copy2 to save it?

2) I don't use job.getbitmapresize because i pass the image to FillImageToView. This sub (based on your snippet) load the bitmap using Bitmap.Resize. Based on this, should I load anyway getBitmapResize and pass it instead bitmap?

3) I tried to download all images at same time, but I understand that is better to wait each download before start next download. I will try. As my sub load a panel, I can't wait for it, so I should use anther sub for download. Is possible to add in a list the resumable sub as object, and call them meanwhile I download the image, to return in the main flow and finish the first sub that I called? I hope I clearly explain what i mean

Thanks for your help!
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
1) It depends on what you are trying to do. The code you posted loads the bitmap and then writes it back. It can be replaced with File.Copy2.

2) Job.GetBitmapResize is more efficient than loading the full bitmap and then resizing it.

3)
I tried to download all images at same time, but I understand that is better to wait each download before start next download
You can download all of them at once. There is no problem with it unless you are loading many images. Just note that the Sleep(0) in the For Each block doesn't do anything useful.
 
Upvote 0
Top