【发布时间】:2014-06-23 10:50:07
【问题描述】:
我的 android 应用正在服务器上上传单个以及多个图像。 如果我上传单张图片则没有问题,但如果我选择多张图片(8-10) 上传到服务器需要很多时间。
请帮帮我。
class asyncImageUploader extends AsyncTask<String, Integer, String>{
String methodName = "UploadImageWithDetail" ;
ProgressDialog progressDialog;
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
progressDialog = LoginActivity.createProgressDialog(CaseDetailsActivity.this);
//progressDialog = new ProgressDialog(CaseDetailsActivity.this);
//progressDialog.show();
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
for(int i = 0 ; i<arrImagePath.size() ; i++)
{
try{
Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
Bitmap bitmap = BitmapFactory.decodeFile(arrImagePath.get(i), options);
bitmap = Bitmap.createScaledBitmap(bitmap, 600, 600, true);
ByteArrayOutputStream buffer = new ByteArrayOutputStream(bitmap.getWidth() * bitmap.getHeight());
bitmap.compress(CompressFormat.PNG, 100, buffer);
imageByteStr = Base64.encodeToString(buffer.toByteArray(), Base64.DEFAULT);
}catch(Exception e){
e.printStackTrace();
}
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(6);
nameValuePairs.add(new BasicNameValuePair("loginID",sharedPreferences.getString("username", "")));
nameValuePairs.add(new BasicNameValuePair("loginKey", sharedPreferences.getString("password", "")));
nameValuePairs.add(new BasicNameValuePair("HostCompID", sharedPreferences.getString("hostID", "")));
nameValuePairs.add(new BasicNameValuePair("LoginCompID", sharedPreferences.getString("logID", "")));
nameValuePairs.add(new BasicNameValuePair("caseID", caseId));
nameValuePairs.add(new BasicNameValuePair("byteImageString", imageByteStr));
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(RxOfficeUtilities.URL+methodName);
try {
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
progressDialog.dismiss();
}
【问题讨论】:
-
但是你的问题是什么......这很明显,如果上传 1 张图片需要 2 秒,那么 10 张图片将需要大约 14 - 20 秒或更长时间......
-
@Rohit 看,单张图片需要 2-3 秒,但上传多张需要 2-3 分钟,进度对话框继续...
-
请详细介绍一下服务器。您没有透露图像必须如何保存在服务器上的名称。
标签: android performance optimization file-upload android-gallery