2

Is there any way in android API 7+ that I can report on the progress of a call to HttpEntity.getContent()?

In my case I am getting an image in the response stream so the transfer can take quite a while. I want to have a ProgressDialog with the style set to STYLE_HORIZONTAL be updated with the progress of the transfer.

Any way to do this?

Thanks!

FlyingStreudel
  • 4,434
  • 4
  • 33
  • 55

1 Answers1

2

Have you tried HttpEntity.getContentLength() to help you predetermine the size of the file? If you use that in conjunction with something like AsyncTask's onProgressUpdate(), you should be able to implement that.

Take a look at this link

Download a file with Android, and showing the progress in a ProgressDialog

It has pretty much what you're looking for. it uses urlConnection to get the InputStream so you would need to adapt that to use HttpEntity. So maybe you'd have something like this.

@Override
protected String doInBackground(String... aurl) {
    int count;
    long contentLength = <yourHttpEntity>.getContentLength();
    InputStream input = new BufferedInputStream(<yourHttpEntity>.getContent());
    OutputStream output = new FileOutputStream("/sdcard/your_photo.jpg");

    byte data[] = new byte[1024];

    long total = 0;
    while ((count = input.read(data)) != -1) {
        total += count;
        publishProgress(""+(int)((total*100)/contentLength));
        output.write(data, 0, count);
    }

    output.flush();
    output.close();
    input.close();
}

and update your dialog in onProgressUpdate.

Community
  • 1
  • 1
Otra
  • 8,108
  • 3
  • 34
  • 49
  • You should indeed give credits to the author of [this](http://stackoverflow.com/questions/3028306/download-a-file-with-android-and-showing-the-progress-in-a-progressdialog/3028660#3028660) answer. – Wroclai Jul 19 '11 at 17:45