使用DownloadManager下载的Android视图图像

时间:2017-01-10 11:39:42

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

我正在使用DownloadManager从互联网下载图像,并希望在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();
                                }
                            }

FileProvider路径:

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

1 个答案:

答案 0 :(得分:0)

我的坏。我设置了意图类型而没有设置数据,所以它清除了Uri。检查原始问题以获得答案。