sgtpotatoe
sgtpotatoe

Reputation: 430

Android using FileProvider to generate URI for File

Since my app targets API>24 I'm having to use FileProvider to handle the Files/Uris.

In this scenario the user can select a media file (video or image) through the following intent:

        val intent = Intent(
        Intent.ACTION_PICK,
        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI
        )
        intent.type = "image/* video/*"
        startActivityForResult(intent, MEDIA_PICKED)

From here I collect the Uri, and save it as a string. Later on, the user can open the media by trigering an intent such as:

        val intent = Intent(Intent.ACTION_VIEW, fileUri)
        intent.setDataAndType(fileUri, "video/*")
        startActivity(intent)

However, I get an error because the Uri needs to be generated through FileProvider since API 24. So after reading through the documentation and several examples, I have an issue with the provider_paths.xml, as an example from documentation:

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

But, as the user selected whicever media on the phone, I have no idea what the name and path values would be in my provider_paths. It varies from phone to phone, and users might be loading a camera video from DCIM or a downloaded video from Downloads. In every example I see users are storing something in their directory e.g. ExternalStorage + "my_app" + "file_name" so they know the path and name of their directory. But how can I use my FileProvider to turn Files into valid URIs if I dont have those Files in a known storage folder?

Upvotes: 2

Views: 5882

Answers (2)

blackapps
blackapps

Reputation: 9282

No. You do not need to use a FileProvider

But you should use ACTION_OPEN_DOCUMENT instead to let the user pick a file.

In onActivityResult you should save the obtained uri and take persistent uri permission on it in order to make it possible to use the uri later.

Then later using ACTION_VIEW use the obtained uri and set a flag FLAG_GRAND_READ_URI_PERMISSION.

Upvotes: 0

Set a manifest like this:

<manifest ...>

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <application ...
        android:requestLegacyExternalStorage="true">
          ...
        <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="${applicationId}.provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths" />
        </provider>

    </application>

</manifest>

And create a xml on app resourses named provider_paths:

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path
        name="external_storage_root"
        path="." />
</paths>

Upvotes: 2

Related Questions