SoFunction
Updated on 2025-04-10

Android FileProvider usage tutorial

Android Basics-FileProvider

Before Android 7.0, the Uri file wasfile:///formProvides access to other apps.

After Android 7.0, for the sake of security,file:///formUri cannot be accessed normally. The official provides FileProvider. Uri generated by FileProvider will be provided byThe form of content://Share with other apps.

So how to use FileProvider?

Declare provider in manifest

	<provider
            android:authorities="${packagename}.provider"
            android:name=""
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name=".FILE_PROVIDER_PATHS"
                android:resource="@xml/file_provider" />
	</provider>
  • authorities: equivalent to a code used for authentication. When sharing file to generate Uri, the corresponding Uri will be generated through its value.
  • The value of exported: is false, which means that the FileProvider can only be used by this application.
  • meta-data: other configuration information
  • grantUriPermissions: Give the receiver's app temporarily permission to operate.

Set up a shared directory

&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;resources&gt;
  &lt;paths&gt;
    &lt;!--&lt;root-path/&gt; Represents the root directory of the devicenew File("/");--&gt;
    &lt;root-path
        name = ""
        path=""/&gt;
    &lt;!-- () + "/path/" --&gt;
    &lt;files-path
        name="my_files"
        path="test/"/&gt;
    &lt;!-- () + "/path/" --&gt;
    &lt;cache-path
        name="my_cache"
        path="test/"/&gt;
    &lt;!-- (null) + "/path/" --&gt;
    &lt;external-files-path
        name="external-files-path"
        path="test/"/&gt;
    &lt;!-- () + "/path/" --&gt;
    &lt;external-cache-path
         name="name"
         path="test/" /&gt;
    &lt;!-- () + "/path/" --&gt;
    &lt;external-path
        name="my_external_path"
        path="test/"/&gt;
    &lt;!-- () + "/path/" --&gt;
    &lt;external-path
        name="files_root"
        path="Android/data/<package name>/"/&gt;
    &lt;!-- pathSet as'.'It means the entire memory card () + "/path/"   --&gt;
    &lt;external-path
        name="external_storage_root"
        path="."/&gt;
  &lt;/paths&gt;
&lt;/resources&gt;

The final generated code effect

Take the second as an example:
content:///my_files/

Generate Content Uri file for use by other apps

File filePath = new File((), "my_log");
File newFile = new File(filePath, "my_log.log");
// Generate UriUri contentUri = (getContext(), "", newFile);

Authorization, generally read and write permissions, and share

// Here is the sending file.Intent intent = new Intent(Intent.ACTION_SEND);
// Set read and write permissions(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
//Uri passes in Intent(Intent.EXTRA_STREAM, contentUri);
startActivity(intent)

This is the end of this article about the Android FileProvider usage tutorial. For more related content on Android FileProvider, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!