Search code examples
androidperformanceoptimizationfile-uploadandroid-gallery

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


My android app is uploading single as well multiple images on server . If i upload single image then there is no problem , but if i select multiple images (8-10) it takes lot of time to upload on server .

Please help me out .

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

    }

Solution

  • I would say that rather than creating a http client in every loop try to use one http client. Also close your buffer at the of each loop or try resetting it.