簡體   English   中英

服務器花費太多時間在android中上傳多個圖像

[英]server takes too much time to upload the multiple images in android

我的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();

    }

我會說,與其在每個循環中創建一個http客戶端,不如嘗試使用一個http客戶端。 此外,請在每個循環的結束時關閉緩沖區,或嘗試將其重置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM