Android Basics-FileProvider
Before Android 7.0, the Uri file wasfile:///form
Provides access to other apps.
After Android 7.0, for the sake of security,file:///form
Uri 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
<?xml version="1.0" encoding="utf-8"?> <resources> <paths> <!--<root-path/> Represents the root directory of the devicenew File("/");--> <root-path name = "" path=""/> <!-- () + "/path/" --> <files-path name="my_files" path="test/"/> <!-- () + "/path/" --> <cache-path name="my_cache" path="test/"/> <!-- (null) + "/path/" --> <external-files-path name="external-files-path" path="test/"/> <!-- () + "/path/" --> <external-cache-path name="name" path="test/" /> <!-- () + "/path/" --> <external-path name="my_external_path" path="test/"/> <!-- () + "/path/" --> <external-path name="files_root" path="Android/data/<package name>/"/> <!-- pathSet as'.'It means the entire memory card () + "/path/" --> <external-path name="external_storage_root" path="."/> </paths> </resources>
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!