我的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();
}
答案 0 :(得分:0)
我想说的是,不是在每个循环中创建一个http客户端,而是尝试使用一个http客户端。 同时关闭每个循环的缓冲区或尝试重置它。