I am downloading an Android app. I am using a LAN connection and the download is very slow.
Here it is the code I use:
URL url = new URL(ep.getFileURL());
File destFile = new File(<path to sd card file>);
URLConnection uCon = url.openConnection();
InputStream is = uCon.getInputStream();
OutputStream os = new FileOutputStream(destFile);
int progress = 0;
int lastProgress = 0;
int totalSize = uCon.getContentLength();
int downloadedSize = 0;
byte[] buffer = new byte[4096];
int count = -1;
while((count = is.read(buffer)) != -1)
{
os.write(buffer, 0, count);
downloadedSize = downloadedSize + count;
progress = (int)(downloadedSize * 100.0 / totalSize);
if(progress - lastProgress >= 5) {
publishProgress(progress);
lastProgress = progress;
}
}
os.close();
Do you find any problems? thank.
Edit:
I checked my code using your suggestions and got the following results:
# Download times tests #
Without bufferedoutput
Downloading file: 1 ms, Start download
Downloading file: 179812 ms, Finished downloading 54687744 bytes
Downloading file: end, 179813 ms
With bufferedoutput
Downloading file: 1 ms, Start download
Downloading file: 178312 ms, Finished downloading 54687744 bytes
Downloading file: end, 178313 ms
With httpclient
Downloading file: begin
Downloading file: 1 ms, Start download
Downloading file: 178241 ms, Finished downloading 54687744 bytes
Downloading file: end, 178242 ms
, HttpClient, ...
, AsyncTask, publishProgress() ...
.