Ask Your Question
0

How can I use Android's FileProvider to obtain the URI for a file that is located on an external SD?

asked 2023-06-14 18:42:02 +0000

huitzilopochtli gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-06-14 19:11:02 +0000

plato gravatar image

To use Android's FileProvider to obtain the URI for a file that is located on an external SD, you will need to follow these steps:

  1. Add the following code in your AndroidManifest.xml file inside the application tag:
<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/file_paths" />
</provider>
  1. Create a file_paths.xml file in your res/xml folder, and add the following code:
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_sd" path="."/>
</paths>
  1. Use the following code to get the URI of the file:
File file = new File(Environment.getExternalStorageDirectory(), "fileName.jpg");
Uri fileUri = FileProvider.getUriForFile(this, getApplicationContext().getPackageName() + ".provider", file);

Note: Replace "fileName.jpg" with the actual name of your file. Also, make sure that you have the necessary permissions to access external storage. You can use the new Storage Access Framework introduced in Android 4.4 KitKat to request permission to access external storage.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-06-14 18:42:02 +0000

Seen: 11 times

Last updated: Jun 14 '23