DownloadManager.addCompletedDownload() 在 Android Q 上弃用
DownloadManager.addCompletedDownload() deprecated on Android Q
关于最近升级到 API 29,我的代码:
downloadManager.addCompletedDownload(downloadFilename,
downloadFilename, true, saveInfo.mimeType,
downloadPath, outputFile.length(), true)
…现在产生弃用警告:
Warning: 'addCompletedDownload(String!, String!, Boolean, String!,
String!, Long, Boolean): Long' is deprecated. Deprecated in Java
DownloadManager.addCompletedDownload 的 API 文档说:
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 documentation is basically non-existent, and the MediaStore 文档没有提供明显的指导。
任何人都可以为上述代码提供 API 29 兼容的替代品吗?
更新
在Android10(Q)中,必须使用MediaStore
概念。
我做了一些测试,似乎使用下载管理器下载的文件(并存储在默认 "Download" 文件夹中)会自动添加到 MediaStore.Downloads
数据库中。因此,您不需要像我在下面描述的那样手动添加它们。对于任何情况,下面都有一段代码,您可以在其中将数据插入 MediaStore.Downloads
原答案
您必须更新 MediaStore.Downloads
集合。这样,您的文件将在 Downloads
文件夹中可见。在 Android Q 中,您不再需要更新 DownloadManager
,而是更新 MediaStore.Downloads
集合。
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)
}
}
注意以下差异
请对此持保留态度,因为我还在检查以下几点:
1 - 不再显示通知。我猜你现在有责任通知用户。
2 - MediaStore.Downloads
合集仅接受“/Downloads”文件夹下的文件。所以,这会影响下载位置。
要确认插入的位置等,您可以按如下方式转储数据库:
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");
}
}
}
关于最近升级到 API 29,我的代码:
downloadManager.addCompletedDownload(downloadFilename,
downloadFilename, true, saveInfo.mimeType,
downloadPath, outputFile.length(), true)
…现在产生弃用警告:
Warning: 'addCompletedDownload(String!, String!, Boolean, String!, String!, Long, Boolean): Long' is deprecated. Deprecated in Java
DownloadManager.addCompletedDownload 的 API 文档说:
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 documentation is basically non-existent, and the MediaStore 文档没有提供明显的指导。
任何人都可以为上述代码提供 API 29 兼容的替代品吗?
更新
在Android10(Q)中,必须使用MediaStore
概念。
我做了一些测试,似乎使用下载管理器下载的文件(并存储在默认 "Download" 文件夹中)会自动添加到 MediaStore.Downloads
数据库中。因此,您不需要像我在下面描述的那样手动添加它们。对于任何情况,下面都有一段代码,您可以在其中将数据插入 MediaStore.Downloads
原答案
您必须更新 MediaStore.Downloads
集合。这样,您的文件将在 Downloads
文件夹中可见。在 Android Q 中,您不再需要更新 DownloadManager
,而是更新 MediaStore.Downloads
集合。
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)
}
}
注意以下差异
请对此持保留态度,因为我还在检查以下几点:
1 - 不再显示通知。我猜你现在有责任通知用户。
2 - MediaStore.Downloads
合集仅接受“/Downloads”文件夹下的文件。所以,这会影响下载位置。
要确认插入的位置等,您可以按如下方式转储数据库:
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");
}
}
}