简体   繁体   中英

Very slow download speeds with this code, any help improving it?

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?

Maybe you would be better served using DownloadManager .
Android will download the file for you. if DownloadProvider does not suit you, You could atleast benchmark your code.

change this

byte data[] = new byte[1024];

to

byte data[] = new byte[4096];

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..!

You are performing IPC every 1KB of download, as you update your Notification . For a 40MB file, this means you are performing approximately 40,000 IPC calls. Please update your Notification much less frequently.

I suspect that the core problem is the network bandwidth / data rate that your (Android phone's) ISP is providing is much less than your PC gets via its LAN / broadband connection. It could be that your phone is getting a weak signal from the local cell, or that the cell is overloaded, or the backbone is overloaded, or that your ISP has poor peering arrangements.

If that is the problem, there's not much you can do about it except (maybe) change phone carriers or reconfigure your (home?) networking so that your phone can use WiFi to talk to your local WiFi router.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM