Answer the question
In order to leave comments, you need to log in
I send the file to the site. I want to see information about the number of bytes transferred. How to do it?
Sending a file to a cloud service. I want to display ProgressDialog
in which the user will see the progress of the file transfer as a percentage. The transfer takes place in AsyncTask doInBackground
.
I send the file like this:
byte[] buf1 = new byte[myfile.available()];
myfile.read(buf1);
myfile.close();
OutputStream os = urlConnection.getOutputStream();
os.write(buf1);
os.flush();
os.close();
urlConnection.getInputStream();
urlConnection.connect();
Answer the question
In order to leave comments, you need to log in
It is necessary to file a `BufferedOutputStream` from the `urlConnection.getOutputStream()` stream, upload the entire file into it, then read it `count` and read from the input stream of the connection, in which the data will appear in case of errors or after a successful upload of the file, in between sleep and update task progress via `publishProgress()`. If I understand the logic of a buffered stream correctly, its counter will reflect the number of bytes not yet transferred to the underlying stream, i.e. at first it will grow to the maximum, then it will fall to zero, and then data will appear in the `urlConnection.getInputStream()` stream, which generally does not need to be simply received and thrown away, but must be read from it. Accordingly, outside you write the progressbar itself and the logic for updating it based on the counter received from inside the task.
PS I will not write the code, I'm not so strong in Java. Here is the main idea, which, in principle, does not depend on the language, but takes into account its features.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question