0

I am trying to show a progress dialog while uploading a file to the server. The problem is that the dialog shows 0% while the file is uploaded then it is dismissed.

public class Complete_Bucket extends AsyncTask<String, Integer, List<String>> {

   @Override
   protected void onPostExecute(final List<String> result) {
        prgDialog.dismiss();
   }

   @Override
   protected void onPreExecute() {
        complete_item.setEnabled(false);
        prgDialog.show();
   }

   @Override
   protected List<String> doInBackground(String... params) {

      String fileName = params[1];
      HttpClient client = new DefaultHttpClient();
      HttpPost post = new HttpPost(complete_bucket);
      MultipartEntityBuilder builder = MultipartEntityBuilder.create();
      builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

      length = 0;
      Bitmap bitmapSelectedImage;
      try {
          if (!fileName.equals("")) {
              bitmapSelectedImage = getSampleBitmapFromFile(fileName, 400, 400);

              ExifInterface exifInterface = new ExifInterface(fileName);
              int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
              int rotationInDegrees = exifToDegrees(orientation);
              Matrix matrix = new Matrix();
              if (rotationInDegrees != 0f) {
                   matrix.preRotate(rotationInDegrees);
                   bitmapSelectedImage = Bitmap.createBitmap(bitmapSelectedImage, 0, 0, bitmapSelectedImage.getWidth(), bitmapSelectedImage.getHeight(), matrix, true);
              }

              ByteArrayOutputStream bos = new ByteArrayOutputStream();
              bitmapSelectedImage.compress(CompressFormat.JPEG, 100, bos);
              ContentBody mimePart = new ByteArrayBody(bos.toByteArray(), fileName);
              builder.addPart("file",mimePart);

              byte[] imageInByte = bos.toByteArray();
              length = imageInByte.length;
        }
        builder.addTextBody("userID", userid);

        final HttpEntity yourEntity = builder.build();

        class ProgressiveEntity implements HttpEntity {

            @Override
            public void consumeContent() throws IOException {
                 yourEntity.consumeContent();
            }
            @Override
            public InputStream getContent() throws IOException,
                                IllegalStateException {
                 return yourEntity.getContent();
            }
            @Override
            public Header getContentEncoding() {
                 return yourEntity.getContentEncoding();
            }
            @Override
            public long getContentLength() {
                 return yourEntity.getContentLength();
            }
            @Override
            public Header getContentType() {
                 return yourEntity.getContentType();
            }
            @Override
            public boolean isChunked() {
                 return yourEntity.isChunked();
            }
            @Override
            public boolean isRepeatable() {
                 return yourEntity.isRepeatable();
            }
            @Override
            public boolean isStreaming() {
                 return yourEntity.isStreaming();
            } 

            @Override
            public void writeTo(OutputStream outstream) throws IOException {

                 class ProxyOutputStream extends FilterOutputStream {

                      public ProxyOutputStream(OutputStream proxy) {
                           super(proxy);
                      }
                      public void write(int idx) throws IOException {
                           out.write(idx);
                      }
                      public void write(byte[] bts) throws IOException {
                           out.write(bts);
                      }
                      public void write(byte[] bts, int st, int end) throws IOException {
                           out.write(bts, st, end);
                      }
                      public void flush() throws IOException {
                           out.flush();
                      }
                      public void close() throws IOException {
                           out.close();
                      }
               }

               class ProgressiveOutputStream extends ProxyOutputStream {
                    long totalSent;
                    long totalSize;
                    public ProgressiveOutputStream(OutputStream proxy, long total) {
                         super(proxy);
                         totalSent = 0;
                         totalSize = total;
                    }
                    public void write(byte[] bts, int st, int end) throws IOException {
                          totalSent += end;
                          publishProgress( (int) ((totalSent / (float) totalSize) * 100));
                          out.write(bts, st, end);
                    }
               }

               yourEntity.writeTo(new ProgressiveOutputStream(outstream, yourEntity.getContentLength()));
                    }

               };

               ProgressiveEntity myEntity = new ProgressiveEntity();

               post.setEntity(myEntity);
               HttpResponse response = client.execute(post);

                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (ClientProtocolException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return null;
            }

            @Override
            protected void onProgressUpdate(Integer... values) {
                super.onProgressUpdate(values);
                prgDialog.setProgress(values[0]);
                Log.i("VALUES", String.valueOf(values[0]));

            }
}
erdomester
  • 11,789
  • 32
  • 132
  • 234
  • The write() where you have publishProgress is never called. Put publishProgress in the other write()s. – greenapps May 28 '15 at 17:24
  • It's called. When I am uploading the image, VALUES are always 0 (logged out in onProgressUpdate). Without the image, I see the progress bar advancing from 0 to 100. – erdomester May 28 '15 at 18:08

0 Answers0