Downloading and processing images using response.GetAsynchronously

Inman

Well-Known Member
Licensed User
Longtime User
I have about 10 images to download. Let them be image1.jpg, image2.jpg......image10.jpg.

I used Response.GetAsynchronously to download the images and I get the update from StreamFinish when the download is complete. Now the issue is the order in which files are downloaded.

I want to download the files in the order from image1 to image10. I have stored the list of urls in an array and use a For Next to give HTTP requests 1 by 1 for download. But the issue is since it says GetAsynchronously, the download completes in random order i.e. sometimes image1 is downloaded first, then image4, then image3 like that.

My question is is it possible to wait for 1 image to download and then proceed to another? Or is it possible to know when all 10 images have been downloaded in any random order?
 

Inman

Well-Known Member
Licensed User
Longtime User
Ok I found a way to check if all the 10 images have been downloaded by incrementing a counter inside StreamFinish and checking if it is 10.

Now the issue is the code works for 5 or 10 images. But when I try to download 25 or 30 images, I get Java exception. The issue is clearly resource shortage. I don't want to download all the 30 images together. I am ready to wait till each image downloads and do the process 1 by 1. But the issue is I have no control over the process. Inside a For loop I call each image one by one on HTTPClient1.Execute. But the Execute event is trying to download all images together and I couldn't find a way to wait for each image to finish download.

Please help.
 
Upvote 0

Inman

Well-Known Member
Licensed User
Longtime User
Found a solution to this issue. Not sure if it is a good practice.

Each time you give an HTTP.Execute, add an HTTP.Initialise just before it.

HttpClient1.Initialize("HttpClient1")
HttpClient1.Execute(request, i)

I thought you need to give HttpClient1.Initialize only once that too in the Sub Activity_Create(FirstTime As Boolean). Fortunately the code works both on emulator and phone.
 
Last edited:
Upvote 0

JogiDroid

Member
Licensed User
Longtime User
How about starting first download and then new download when StreamFinish happens (.. that way images would load one by one in that order you want?
 
Upvote 0

Inman

Well-Known Member
Licensed User
Longtime User
Thanks for the tip. I added a download sub that can be called sequentially from StreamFinish. Seems to work fine although the process being synchronous now has slowed down the image download. Still it works and feels more stable.
 
Upvote 0

Inman

Well-Known Member
Licensed User
Longtime User
Yes I did and 9 images work fine on my app too. In fact I think it is ok upto may be 15. But one time I had 35 images to download and I had no choice but to download each in a loop. That sometimes work but sometimes I get a Java Rejected exception.
 
Upvote 0

Inman

Well-Known Member
Licensed User
Longtime User
That explains it. So I should try and increase the current 1 by 1 download rate to may be 15 concurrent downloads.
 
Upvote 0
Top