Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DownloadManager.addCompletedDownload() deprecated on Android Q

On upgrading recently to API 29, my code:

downloadManager.addCompletedDownload(downloadFilename,
    downloadFilename, true, saveInfo.mimeType,
    downloadPath, outputFile.length(), true)

…now produces a deprecation warning:

Warning: 'addCompletedDownload(String!, String!, Boolean, String!, String!, Long, Boolean): Long' is deprecated. Deprecated in Java

The API documentation for DownloadManager.addCompletedDownload says:

This method was deprecated in API level 29. Apps should instead contribute files to MediaStore.Downloads collection to make them available to user as part of Downloads.

However, I have been unable to find a code example of how exactly MediaStore.Downloads should be used as a replacement. The MediaStore.Downloads documentation is basically non-existent, and the MediaStore documentation provides no obvious guidance.

Can anyone provide an API 29-compliant replacement for the above code?

like image 464
Stephen Talley Avatar asked Aug 07 '19 17:08

Stephen Talley


1 Answers

Update

In Android 10 (Q), you must use the MediaStore concept. I did some tests and it seems that downloaded files using Download Manager (and stored at default "Download" folder) are automatically added to MediaStore.Downloads database. So, you don't need to manually add them as I described below. For any case, below there's a piece of code where you can insert data into MediaStore.Downloads

Original Answer

You must update the MediaStore.Downloads collection. This way, your file will be visible in the Downloads folder. In Android Q, you no longer need to update DownloadManager but the MediaStore.Downloads collection.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
    // You can add more columns.. Complete list of columns can be found at 
    // https://developer.android.com/reference/android/provider/MediaStore.Downloads
    ContentValues contentValues = new ContentValues();
    contentValues.put(MediaStore.Downloads.TITLE, /* FILE_NAME */);
    contentValues.put(MediaStore.Downloads.DISPLAY_NAME, /* DISPLAY NAME */);
    contentValues.put(MediaStore.Downloads.MIME_TYPE, /* MIME TYPE */);
    contentValues.put(MediaStore.Downloads.SIZE, /* FILE SIZE */);

    // If you downloaded to a specific folder inside "Downloads" folder
    contentValues.put(MediaStore.Downloads.RELATIVE_PATH, Environment.DIRECTORY_DOWNLOADS + File.separator + "Temp");

    // Insert into the database
    ContentResolver database = getContentResolver();
    database.insert(Downloads.EXTERNAL_CONTENT_URI, contentValues);
} else {
    DownloadManager downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
    if (downloadManager != null) {
        downloadManager.addCompletedDownload(downloadFilename, downloadFilename, true, 
                                  saveInfo.mimeType, downloadPath, outputFile.length(), true)
    }
}

NOTE following differences

Take this is with a grain of salt since I'm also still checking below points:

1 - Notification is no longer displayed. I guess you are now responsible to notify the user now.

2 - MediaStore.Downloads collection accepts files under the "/Downloads" folder only. So, this can affect the download location.

To confirm where inserted etc, you can dump the database as follows:

public void dumpDb() {
    DownloadManager downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
    if (downloadManager != null) {
        Cursor cursor = downloadManager.query(new DownloadManager.Query());
        Log.e("TESTS", "DownloadManager dump start");
        while(cursor.moveToNext()) {
            Log.e("TESTS", "Title: " + cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_TITLE))
                    + " status: " + cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS))
                    + " id: " + cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_ID)));
        }
        Log.e("TESTS", "DownloadManager dump end");
        cursor.close();
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        ContentResolver contentResolver = getContentResolver();
        if (contentResolver != null) {
            Cursor cursor = contentResolver.query(Downloads.EXTERNAL_CONTENT_URI, null, null, null);
            Log.e("TESTS", "MediaStore Collection Dump start");
            if (cursor != null) {
                Log.e("TESTS", DatabaseUtils.dumpCursorToString(cursor));
                cursor.close();
            }

            Log.e("TESTS", "MediaStore Collection Dump end");
        }
    }
}
like image 129
W0rmH0le Avatar answered Oct 18 '22 02:10

W0rmH0le