使用下载管理器下载的Android视图图像



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

这是我的代码:

                            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>

并显示:

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

我的错。我在没有设置数据的情况下设置了意图类型,因此它清除了 Uri。检查原始问题以获取答案。

相关内容

  • 没有找到相关文章

最新更新