I'm working on a project which download a file by using a http connection. I display a horizontal progress bar with the progress bar status during the downloading. My function looks like this:
.......
try {
InputStream myInput = urlconnect.getInputStream();
BufferedInputStream buffinput = new BufferedInputStream(myInput);
ByteArrayBuffer baf = new ByteArrayBuffer(capacity);
int current = 0;
while((current = buffinput.read()) != -1) {
baf.append((byte) current);
}
File outputfile = new File(createRepertory(app, 0), Filename);
FileOutputStream myOutPut = new FileOutputStream(outputfile);
myOutPut.write(baf.toByteArray());
...
}
I know in advance the size of my file so I need to retrieve the size during the downloading (in my while block). Thus I'll be able to determinate the status of the progress bar.
progressBarStatus = ((int) downloadFileHttp(url, app) * 100)/sizefile;
long downloadFileHttp(.., ..) is the name of my function.
I already try to retrieve it by using outputfile.length but his value is "1" maybe it's the number of file that I'm trying to download.
Is there any way to figure it out?
UPDATE 1
I haven't got any thread which allow me to figure this out. Currently I have got a horizontal progress bar whch displays only 0 and 100% whitout intermediate values. I think about another approach. If I know the rate of my wifi and the size of the file I can determinate the time of downloading.
I know that I can retrieve the piece of information of my Wifi connection and the size of my file to download.
Is anybody already have worked or have thread on it?