04-19-2012 06:35 AM
My requirement is that the user captures images and the images needs to be uploaded to the server in the background while the user can continue to perform other tasks with the application. I would like to know what is the best way to implement a background thread which uploads images to the server.Could someone kindly provide me with some inputs. Thanks in advance.
Solved! Go to Solution.
04-19-2012 07:42 AM
There is no problem with the Background Thread. I would recommend that you have just one Thread, which picks up the first element of a persisted list of the images to be sent. Once the image it has been sent, the Thread deletes the entry for the one just sent, and picks up the next one. You need proper synchronization round the updating of the list, I typically use Vector which is in theory synchronized properly but I do a check anyway.
The Thread processing has a public add method which will start the Thread if it is not currently running. The Thread stops when there is nothing in the queue.
All this is pretty standard I think. But there a few things that I think you may need to add to this processing to make it more robust:
a) You may give the user the option to delete the image once it is sent. Remember in fact that the user could delete the image before it is sent and so the processing must take care of that.
b) I would recommend a Server component to this that receives the Images. You can then pass the image in chunks because some communication methods have limits on the size of data that can be transferred in one connection. I would recommend breaking the image up into 64 KB chunks. You should include a restart mechanism in here too, so that you can resend just the unsent parts of an image after a failure.
c) You need to think about whether the server and the intervening gateways will process binary data, or do you have to encode it into text (Base 64 seems like the way to go if you do).
d) You may consider allowing the user to restrict communication to specific connections, like WiFi and/or 3G.
e) You will need to be very careful about coding restart and recovery, so that the processing does not die unexpectedly.
Hope this is useful. .
04-19-2012 11:50 PM
Thank you for your quick response. This was really really helpful. I use the Base 64 route to send the images to the server. I noticed that when the picture size is set to 2048 x 1536, the image does not get uploaded to the server and remains in the queue, however with the picture size 1024 x 768 or 480 x 360 the image gets uploaded within no time to the server. I use HttpConnection inorder to transmit the image. Should i be reducing the size of the image when the size is set to 2048 x 1536? Am i missing out on something here? Kindly advice.
04-20-2012 04:16 AM
You need to track the error that is stopping your larger images being sent to know what to do to rectify the problem. However one concern is the size of the image. In terms of raw pixels, the 2048 x 1536 image size is 4 times as many pixels as the1024 x 768 and it could be that you have exceeded some size limit.
As to whether you should scale or not, that depends on your requirement. Certainly I would recommend that you scale on device if you can to reduce the data being sent. Scaling is not that difficult, though the best approach depends on what level of OS you are targeting.
04-20-2012 08:46 AM
There are issues if you have transparent images, in which case look at this: