首页 文章

如何在图库中打开文件(来自SD卡)

提问于
浏览
0

我保存了不同jpg的路径 . 数据库中的文件 . 这些图像部分位于内部存储器中,部分位于外部存储器(SD卡)中 .

现在我想打开画廊中的图片 . 要从内部存储器打开图像,我使用FileProvider,我按如下方式继续:

String path = "/storage/emulated/0/Pictures/Meetings/IMG_Testmeeting20180809_225414.jpg";

File image = new File(path);
Uri uri = FileProvider.getUriForFile(getContext(), "com.meetings.martin.provider",image);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "image/*");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
getContext().startActivity(intent);

这是我在AndroidManifest.xml中的FileProvider:

<provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="com.meetings.martin.provider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/file_paths">
    </meta-data>
</provider>

这是file_paths.xml的内容:

<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="my_images" path="." />
</paths>

这完美地工作.. ..但是,如果我想打开位于SD卡上的图像,我收到以下错误消息:

Failed to find configured root that contains <path>

我经常读到FileProvider无法访问SD卡上的文件 . But how can I then turn the path into a Uri and open it in the gallery?

1 回答

  • 0

    我解决了我在路径文件中添加了以下行:

    <root-path name="external_files" path="/storage/" />
    

相关问题