Android 查看用 DownloadManager 下载的图片

标签 android android-intent download-manager android-fileprovider

我正在使用 DownloadManager 从 Internet 下载图像,并希望在 Gallery 应用程序中显示它们。我将图像保存到默认的“下载”目录。下载工作正常,我收到成功通知。图库应用打开,但不显示图像。可能是什么问题?

这是我的代码:

                            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>

和 list :

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

最佳答案

我的错。我在没有设置数据的情况下设置了 Intent 类型,所以它清除了 Uri。检查原始问题的答案。

关于Android 查看用 DownloadManager 下载的图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41567946/

相关文章:

java - 在本地部署 App Engine 连接的 Android

android - 如何更改手机/模拟器上的标准资源

android - 在 api 17 之前从 RelativeLayout 中删除规则

android - 自定义 HDPI 控件不可见

android - 将本地文件附加到电子邮件

java - 为下载管理器添加 HTTPS 支持

android - 控制android中下载管理器的下载顺序

android - onCreate() 中的 extra 为空值

android - 使用 Intent 调用电话时需要权限吗?

安卓下载管理器