Mat_R
Mat_R

Reputation: 11

How to open files (from SD-Card) in gallery

I save paths of different jpg. files in a database. These images are partly in the internal memory and partly in the external memory (SD-card).

Now I want to open the pictures in the gallery. To open images from the internal memory I use the FileProvider, I proceed as follows:

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

This is my FileProvider in the AndroidManifest.xml:

<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>

This is content of the file_paths.xml:

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

This works perfectly.. ..but , if I want to open an image, which is located on the SD card, I get the following error message:

Failed to find configured root that contains <path>

I've often read that the FileProvider can not access files on the SD card. But how can I then turn the path into a Uri and open it in the gallery?

Upvotes: 0

Views: 5403

Answers (1)

Mat_R
Mat_R

Reputation: 11

I solved it. I added following line to the path file:

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

Upvotes: 1

Related Questions