DownloadManager.addCompletedDownload() deprecated on Android Q

前端 未结 1 1852
情话喂你
情话喂你 2021-01-01 19:47

On upgrading recently to API 29, my code:

downloadManager.addCompletedDownload(downloadFilename,
    downloadFilename, true, saveInfo.mimeType,
    downloadP         


        
相关标签:
1条回答
  • 2021-01-01 20:25

    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");
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题