使用DownloadManager下载的Android view图像
我正在使用DownloadManager从internet下载图像,并希望在Gallery应用程序中显示它们。我正在将图像保存到默认的“下载”目录。下载效果很好,我收到了成功通知。gallery应用程序将打开,但不显示图像。有什么问题吗 这是我的密码:使用DownloadManager下载的Android view图像,android,android-intent,download-manager,android-fileprovider,Android,Android Intent,Download Manager,Android Fileprovider,我正在使用DownloadManager从internet下载图像,并希望在Gallery应用程序中显示它们。我正在将图像保存到默认的“下载”目录。下载效果很好,我收到了成功通知。gallery应用程序将打开,但不显示图像。有什么问题吗 这是我的密码: Cursor cursor = ((DownloadManager) getSystemService(DOWNLOAD_SERVICE)).query(ImageDownloadQuery
Cursor cursor = ((DownloadManager) getSystemService(DOWNLOAD_SERVICE)).query(ImageDownloadQuery);
if (cursor.moveToFirst()) {
String path = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
File file = new File(URI.create(path));
Uri uri = FileProvider.getUriForFile(ConversationDetailsActivity.this,
BuildConfig.APPLICATION_ID + ".fileprovider",
file);
Intent viewIntent = new Intent(Intent.ACTION_VIEW, uri);
viewIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
viewIntent.setType(cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_MEDIA_TYPE)));
if (getPackageManager().resolveActivity(viewIntent, 0) != null) { // checking if there is an app installed that can handle this type of files
startActivity(viewIntent);
} else { // app that can view this file type is not found
Toast.makeText(getBaseContext(), "Please install an application to view this type of files", Toast.LENGTH_SHORT).show();
}
}
文件提供程序路径:
<paths>
<cache-path
name="cache"
path=""
/>
<external-path
name="download"
path="Download/"
/>
</paths>
我的错。我在没有设置数据的情况下设置了intent类型,因此它清除了Uri。检查原始问题的答案
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true"
>
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"
/>
</provider>
DownloadManager.Query ImageDownloadQuery = new DownloadManager.Query();
ImageDownloadQuery.setFilterById(referenceId);
Cursor cursor = ((DownloadManager) getSystemService(DOWNLOAD_SERVICE)).query(ImageDownloadQuery);
if (cursor.moveToFirst()) {
String path = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
File file = new File(URI.create(path));
Uri uri = FileProvider.getUriForFile(ConversationDetailsActivity.this,
BuildConfig.APPLICATION_ID + ".fileprovider",
file);
Intent viewIntent = new Intent(Intent.ACTION_VIEW);
viewIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
viewIntent.setDataAndType(uri, cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_MEDIA_TYPE)));
if (getPackageManager().resolveActivity(viewIntent, 0) != null) { // checking if there is an app installed that can handle this type of files
startActivity(viewIntent);
} else { // app that can view this file type is not found
Toast.makeText(getBaseContext(), "Please install an application to view this type of files", Toast.LENGTH_SHORT).show();
}
}