Android将图片上传到服务器的有效方法 [英] android efficient way to upload image to server

查看:1423
本文介绍了Android将图片上传到服务器的有效方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种将图像从android上传到php服务器的方法,目前我正在将图像编码为base64并发送给它,但是它太慢了,有没有更好的方法

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.

推荐答案

我的2美分:

根据您要定位的目标,几乎没有什么可以改进的.

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

  1. 如果您担心数据使用,则可以在上传之前减小图像的大小.对于较新的手机型号来说,现在的图片分辨率非常高.上传之前,您可以在设备本身上更改图像的大小.例如:如何将图像(位图)调整为给定大小? 在Android中减小图像尺寸而不损失其质量
  2. 如果您担心在上传图片时阻止用户:
    • 在后台执行上传部分,以免妨碍用户执行任何操作.
    • 还可以按照注释中的建议进行多部分上传,以防万一上传出现问题,您不必再次上传整个内容.
  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.
  • 您需要更好的网络连接或减小图像尺寸.如果您知道大多数用户位于较低的连接区域中,那么减小大小可能会很好.
  • 您可以使用某些库,例如 Facebook的网络连接 质量检查员 课检查 尝试上载并可能等待时的当前网络质量 上传之前先建立一个高质量的网络.
  • 您可以选择仅在wi-fi上上传,然后使用BroadcastReceiver收听其状态更改. 例如: 当wifi或3g网络状态更改时的广播接收器
  • 查看截击是否支持gzip压缩.如果确实如此,请确保您正在压缩请求.这样,您可以压缩请求数据,这可以极大地改善大小. Android Volley:gzip响应
  • 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

希望这会有所帮助.

这篇关于Android将图片上传到服务器的有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆