android efficient way to upload image to server

2020-06-28 16:14发布

问题:

I'm looking for a way to upload an image from android to php server, currently I'm encoding the image to base64 and send it, but it's too slow, is there is a better way

I'm using volley as network client.

回答1:

My 2 cents:

Few things can be improved depending on what you are targeting.

  1. If you are worried about the data usage, then you can decrease the size of the image before uploading. Now a days for the newer phone models, the picture resolutions are very high. You can alter the size of the image on the device itself before uploading. Eg: How to resize image (Bitmap) to a given size? and Decrease image size without losing its quality in android
  2. If you are worried about blocking user when the images are uploading:
    • Do the uploading part in the background, so that you don't block the user from doing anything.
    • Also as suggested in comments you can do a multi-part upload so that in case something goes wrong with the upload, you don't have to upload the whole thing again.
  3. If you are only worried about the time taken to upload:
    • You either need a better network connectivity or lower the image size. If you know most of your users will be in lower connectivity areas, then it might be fine to decrease size.
    • You can use some library like Facebook's Network Connection Quality Checker Class to check the current network quality when trying to upload and maybe wait for a good quality network before uploading.
    • You can choose to only upload on wi-fi and use BroadcastReceiver to listen for the change in its state. Eg: BroadcastReceiver when wifi or 3g network state changed
    • See if volley supports gzip compression. If it does make sure you are compressing the requests. This way you are zipping your request data which can be a substantial improvement in the size. Android Volley: gzip response

Hope this helps.