in my Android app I need to download a ~40 MB file, and I’m testing the code in my phone but the download speed is REALLY slow, I tried to download from different sources that are fast when using my PC.
Here is the code I use in the download service:
URLConnection conexion;
URL url;
int lenghtOfFile = 0;
try {
url = new URL("<URL>");
conexion = url.openConnection();
conexion.connect();
lenghtOfFile = conexion.getContentLength();
try {
File f = new File(Environment.getExternalStorageDirectory() + "/testing");
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(f.getAbsolutePath());
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
notification.contentView.setProgressBar(R.id.status_progress, 100, (int) (total * 100 / lenghtOfFile), false);
notification.contentView.setTextViewText(R.id.status_text, Long.toString(total * 100 / lenghtOfFile));
notificationManager.notify(42, notification);
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {
e.getCause();
e.getMessage();
}
} catch (Exception e) {
}
}
Is it possible that this code is the reason of the slow dl speeds? Any ideas on how to make the download speed faster?
change this
to
and as commonsware said,
update your download progress notification in less frequencies.
for eg:
use a simple counter variable in your loop, and update progress when it reaches 10, and then resetting it..!