将图片一张一张上传到服务器 - Android
Upload images to server one by one - Android
我想知道如何实现将图像列表上传到服务器,但最重要的是一张一张地上传。
下面是包含带有图像列表的地图的代码,我想遍历列表并一次上传一张图像。
如果可能,不使用定时器。如果有任何建议,我将不胜感激。
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();
}
});
}
尝试使用 Handler.postDelayed()
要一张一张地 post 您的图像,您可能需要使用同步请求,但您可以在 UI 线程上执行此操作。所以你应该在背景中这样做。例如,您可以使用 AsyncTask.
由于没有更好的解决方案,我设置了loopj Async Http Client的onSuccess()方法,在上传图片后自动触发父方法。同时,我增加了即将到来的文件在我用来存储每个图像路径的数组中的位置。
效果很好,希望能帮到有同样问题的人。
public void uploadPhoto(){
String iPath = files[position];
String iName = imgName[position];
final int DEFAULT_TIMEOUT = 30000;
AsyncHttpClient client = new AsyncHttpClient();
client.setTimeout(DEFAULT_TIMEOUT);
File imageFile = new File(imagePath);
try {
params.put("imageFile", imageFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
params.put("position", position);
params.put("name", name.getText());
client.post("http://www.yoursite.com/upload-img.php", params,
new AsyncHttpResponseHandler() {
@Override
public void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3) {
uploadPhoto();
}
@Override
public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
position ++;
if(position < files.length) {
uploadPhoto();
} else {
progress.dismiss();
Toast.makeText(context, "Thanks for your order!", Toast.LENGTH_LONG).show();
finish();
}
}
});
}
我想知道如何实现将图像列表上传到服务器,但最重要的是一张一张地上传。
下面是包含带有图像列表的地图的代码,我想遍历列表并一次上传一张图像。
如果可能,不使用定时器。如果有任何建议,我将不胜感激。
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();
}
});
}
尝试使用 Handler.postDelayed()
要一张一张地 post 您的图像,您可能需要使用同步请求,但您可以在 UI 线程上执行此操作。所以你应该在背景中这样做。例如,您可以使用 AsyncTask.
由于没有更好的解决方案,我设置了loopj Async Http Client的onSuccess()方法,在上传图片后自动触发父方法。同时,我增加了即将到来的文件在我用来存储每个图像路径的数组中的位置。
效果很好,希望能帮到有同样问题的人。
public void uploadPhoto(){
String iPath = files[position];
String iName = imgName[position];
final int DEFAULT_TIMEOUT = 30000;
AsyncHttpClient client = new AsyncHttpClient();
client.setTimeout(DEFAULT_TIMEOUT);
File imageFile = new File(imagePath);
try {
params.put("imageFile", imageFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
params.put("position", position);
params.put("name", name.getText());
client.post("http://www.yoursite.com/upload-img.php", params,
new AsyncHttpResponseHandler() {
@Override
public void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3) {
uploadPhoto();
}
@Override
public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
position ++;
if(position < files.length) {
uploadPhoto();
} else {
progress.dismiss();
Toast.makeText(context, "Thanks for your order!", Toast.LENGTH_LONG).show();
finish();
}
}
});
}