W
W
wolfak2016-06-09 15:32:14
Java
wolfak, 2016-06-09 15:32:14

How to get progress when uploading a file via HttpRequest Android?

Good afternoon. Faced another problem. I'm uploading a file in an Android app using an HttpRequest in an AsyncTask and would like to show users the upload process. Example: File upload: 50%
I know how to update the counter in AsyncTask, but how do I get the progress of uploading a file to the server in HttpRequest?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eugene, 2016-06-09
@Ragnar0k

I somehow implemented something similar, though without HttpRequest
Maybe something will help you

@Override
        protected Bitmap doInBackground(String... params) {
            int count;
            try {

                URL url = new URL(params[0]);
                URLConnection connect = url.openConnection();
                connect.connect();

                int lengthOfFile = connect.getContentLength();
                input = new BufferedInputStream(url.openStream());
                output = new FileOutputStream("sdcard/downloaded_photo.jpg");


                byte data[] = new byte[1024];

                long total = 0;

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

                }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question