【发布时间】:2015-10-04 13:13:25
【问题描述】:
我想知道如何实现将图像列表上传到服务器,但最重要的是一张一张。
下面是包含带有图像列表的地图的代码,我想遍历列表并一次上传一张图像。
如果可能,不使用计时器。如果有任何建议,我将不胜感激。
sendOrder.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
for (Map.Entry<String,String> entry : imgPathAndNameMap.entrySet()) {
uploadPhoto(entry.getKey(), entry.getValue());
}
}
}
});
public void uploadPhoto(String imgName, String imgPath){
final int DEFAULT_TIMEOUT = 30000;
AsyncHttpClient client = new AsyncHttpClient();
client.setTimeout(DEFAULT_TIMEOUT);
RequestParams params = new RequestParams();
//check image size
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true;
Bitmap bp = BitmapFactory.decodeFile(imgPath, opts);
//get the original size
int orignalHeight = opts.outHeight;
int orignalWidth = opts.outWidth;
Log.w("", orignalHeight + "x" + orignalWidth);
if(orignalHeight > 2000 || orignalWidth > 2000){
byte[] bitmapdata = resize(imgPath, orignalHeight, orignalWidth, opts, bp);
params.put("zipFile", new ByteArrayInputStream(bitmapdata), imgName);
} else {
File theZip = new File(imgPath);
try {
Log.w("", imgPath );
params.put("zipFile", theZip);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
params.put("name", name.getText());
client.post("http://www.lab.com/upload-image.php", params,
new AsyncHttpResponseHandler() {
@Override
public void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3) {
if (arg0 == 0) {
}
}
@Override
public void onProgress(int position, int length) {
try {
((PreloaderDialog) progressDialog).passValues(
position, length);
} catch (Exception e) {
}
}
@Override
public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
Toast.makeText(context, "Multumim pentru Comanda!", Toast.LENGTH_LONG).show();
}
});
}
【问题讨论】:
-
最好的选择是 Volley - 谷歌一下。
-
你可以再次使用AsyncTask,OnPost方法,你可以用不同的图像启动asyncTask,而无需与UI线程交互。否则你可能会遇到UI冻结问题。
标签: android image list loops file-upload