0

I'm starting a 2Gb file download using the DownloadManager, and in doing so I offer the user the option to pick where they want to save that file: internal storage or SD card. Also, I give the user an option to cancel said download. Here is my code:

DownloadManager.Query query = new DownloadManager.Query();
query.setFilterById(downloadId);
Cursor cursor = downloadManager.query(query);

try { 
    if (cursor.moveToFirst()) {
        int status =cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
        if (status == DownloadManager.STATUS_RUNNING || 
                status == DownloadManager.STATUS_PENDING) {
            downloadManager.remove(downloadId);
        } 
    } 
} finally { 
    cursor.close();
} 

My problem: when downloading to internal storage, the .remove(downloadId) call works instantly, and the download stops. But when downloading to the SD Card (on a Samsung Galaxy S7 Edge), using the same code, it takes 3-5 minutes to stop the download.

Any ideas on removing that delay?

Edit: some paths examples, returned by Activity.getExternalFilesDirs():

/storage/emulated/.../ - this is internal

/storage/0123-4567/.../ - this is the SD Card

zrgiu
  • 6,200
  • 1
  • 33
  • 35
  • Can you give examples of paths you consider to be on internal storage and on SD card? – greenapps Oct 25 '16 at 13:11
  • just edited the question to add the paths – zrgiu Oct 25 '16 at 13:35
  • /storage/emulated/0/...... is called external storage. Obtained with getExternalStorageDirectory(). But i do not see a solution. sorry. Did you test other devices? – greenapps Oct 25 '16 at 13:39
  • both are called "external storage", but the first one is actually internal. Hence the "emulated" in the path. I have tested with other devices - same behavior – zrgiu Oct 25 '16 at 13:42
  • You can reach internal storage with getFilesDir(); – greenapps Oct 25 '16 at 13:43
  • `it takes 3-5 minutes`. And how much would a complete download take? You took a 2GB file. But say you take a much smaller one? – greenapps Oct 25 '16 at 13:45

1 Answers1

0

Found the answer for myself: it looks like before the download starts, the DownloadManager creates a 2Gb file (the size of the file I'm downloading) filled with zeros so that it keeps the storage allocated. That file-write operation is what's making this slow on a SD Card.

The internal storage on the S7 Edge is crazy-fast, which is why the problem is not present there. The actual problem (an OS bug in my opinion) is that calling .remove() waits for that initial write operation instead of cancelling it.

zrgiu
  • 6,200
  • 1
  • 33
  • 35