【问题标题】:Android view image downloaded with DownloadManager使用 DownloadManager 下载的 Android 查看图像
【发布时间】:2017-01-10 11:39:41
【问题描述】:

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

这是我的代码:

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

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-04
      • 2015-05-16
      相关资源
      最近更新 更多