在Android Q上不推荐使用DownloadManager.addCompletedDownload() [英] DownloadManager.addCompletedDownload() deprecated on Android Q

查看:250
本文介绍了在Android Q上不推荐使用DownloadManager.addCompletedDownload()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在最近升级到API 29时,我的代码是

On upgrading recently to API 29, my code:

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

…现在会产生弃用警告:

…now produces a deprecation warning:

警告:'addCompletedDownload(String !, String !, Boolean,String !,String !, Long,Boolean):不建议使用Long'.在Java中已弃用

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

DownloadManager.addCompletedDownload的API文档说:

The API documentation for DownloadManager.addCompletedDownload says:

此方法在API级别29中已弃用.应用程序应该将文件贡献给MediaStore.Downloads集合,以使用户可以将它们作为下载"的一部分使用.

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.

但是,我无法找到有关MediaStore.Downloads应该如何精确替代的代码示例. MediaStore.Downloads 文档基本上不存在,并且 MediaStore 文档没有提供明显的指导.

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.

任何人都可以为上述代码提供API 29兼容的替代品吗?

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

推荐答案

更新

在Android 10(Q)中,您必须使用 MediaStore 概念.我做了一些测试,看来使用下载管理器"下载的文件(并存储在默认的下载"文件夹中)会自动添加到 MediaStore.Downloads 数据库中.因此,您无需按照我的说明手动添加它们.无论如何,下面都有一段代码,您可以在其中将数据插入 MediaStore.Downloads

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

原始答案

您必须更新 MediaStore.Downloads 集合.这样,您的文件将显示在 Downloads 文件夹中.在Android Q中,您不再需要更新 DownloadManager ,而是要更新 MediaStore.Downloads 集合.

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)
    }
}

注意以下差异

把它和一粒盐一起吃,因为我还在检查以下几点:

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

1-不再显示通知.我想您现在有责任立即通知用户.

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

2- MediaStore.Downloads 集合仅接受"/Downloads"文件夹下的文件.因此,这可能会影响下载位置.

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");
        }
    }
}

这篇关于在Android Q上不推荐使用DownloadManager.addCompletedDownload()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆