Send the file to the website. Want to see information about the number of bytes transferred. How to do it?

Send the file to a cloud service. Want to display ProgressDialogin which the user will see the transfer progress of the file in percentage. Transmission occurs in AsyncTask doInBackground.
File give 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();

Maybe someone knows how it can be implemented?
Thanks in advance!
June 26th 19 at 14:17
1 answer
June 26th 19 at 14:19
Need gash `BufferedOutputStream` from the stream `urlConnection.getOutputStream ()` in it to fill the entire file, then read it `count` and read from the input stream of the connection in which the data appears in error or after a successful pouring of the file, in between sleep and update task progress using `publishProgress()`. If I understand correctly the logic buffered stream, its counter will reflect the number of not yet transferred to the underlying stream of bytes, i.e. it will initially increase to maximum and then will fall to zero, and then will appear the data in the stream `urlConnection.getInputStream ()` which actually do not just obtain and throw and need to read. Accordingly, the outside I want the progress bar and the logic updates based on the inside tasks of the counter.

PS the code will not write, not so strong in Java. Here is the basic idea, which in principle is language independent, but takes into account its features.
Do not really understand about the count byte (the count) . How do I count the number of not transferred bytes of BufferedOutputStream? - Alexandro76 commented on June 26th 19 at 14:22
I tried to read bytes in a loop for. In which using the method write, writing the bytes to BufferedOutputStream. This allowed me to count the number of bytes sent. But if the file is "large" then the cycle spent a lot of time - Alexandro76 commented on June 26th 19 at 14:25
"How do I count the number of not transferred bytes of BufferedOutputStream? "
Post it count as task progress.

"But if the file is "big" on the cycle spent much time "
To be considered it will be, but the principle is different. In such a loop are considered to be the bytes written to the buffer and not transmitted to the socket, and you want to count the transferred bytes. Therefore, I suggest to fill buffer the whole file at once, then sleep, read, count, publish it, and watch, and if the stream of urlConnection.getInputStream() the data. If so, the transfer is completed, data being processed and fall out of doInBackground(). Well what if the error is thrown, also have to handle. If buffered stream doesn't know how to count the bytes to the underlying output stream has not yet passed, then nothing you have not really come out, because there are no opportunities to obtain data of interest. Anyway, I have other APIs that return something similar to the bytes in https://developer.android.com/reference/java/net/U... is not found. - sadie_Hop commented on June 26th 19 at 14:28

Find more questions by tags Android