Copyright Notice:
This account published articles are from the public account, Chengxiang Moying (cxmyDev), chengxiang Moying all rights reserved.
Shall not be reproduced without permission.
One, foreword
Starting with Android N (7.0), StrictMode will be strictly enforced, that is, security checks will be more stringent. From Android N, it will not be allowed to pass a File between apps using the File :// method. Otherwise, it will throw a FileUriExposedException error, which will directly cause Crash.
However, since the official government has made such a drastic change to file sharing, it actually provides a solution: the FileProvider, which replaces the file:// with the comtent:// mode, in the meantime, This policy requires the developer to actively upgrade targetSdkVersion to 24, giving the developer time to upgrade.
If you don’t know about targetSdkVersion configured by Android for compatibility, check out the previous article.
This article provides a detailed explanation of all the details you need to know about FileProviders.
How to use FileProvider
1. What is FileProvider
FileProvider is a subclass of ContentProvider provided under The Android Support V4 package. It is used to share files with other apps under the V4 package. Therefore, once the V4 package is introduced, full version compatibility can be achieved.
Since FileProvider is essentially a ContentProvider, it also inherits the features of ContentProvider. A ContentProvider is basically a way to share data with other external apps in a controlled way. The FileProvider turns this data into a File.
2. In what scenarios should FileProvider be used
This restriction applies to all intents that contain the file:// uris leaving your App. This restriction applies to all intents that contain the file:// uris leaving your App. This restriction applies to all intents that contain the file:// uris leaving your App. So, whenever you pass a file:// Uri through an Intent in your App, be careful.
In the actual development process, the most commonly used scenarios are as follows:
- Call the camera to take a picture.
- Crop the picture.
- Call system installer to install Apk.
3. How to use FileProvider
1) Configure in AndroidManifest
As mentioned earlier, FileProvider is actually a ContentProvider, so if you want to use it, you need to declare it in androidmanifest.xml.
- Name: Configures the current FileProvider implementation class.
- Authorities: Configures the name of a FileProvider that must be unique in the current system.
- Exported: Indicates whether the FileProvider needs to be exported. False because it does not.
- GranUriPermissions: Whether temporary access to the authorized file is allowed. This is required, so true.
As you can see, the name attribute is the implementation class that marks the current FileProvider. For an App Module, if it is used by itself, it can directly use the V4 package FileProvider. However, if you want to use it as a Lib Module for other projects, it’s better to re-inherit a FileProvider and fill in our inherited class.
2) Specify a path for sharing files
When configuring the Provider, you also need to configure an additional
files-path
These configurations, in the FileProvider source code, are marked with each TAG_Xxx.
- Root-path: indicates the root directory (/).
- Files-path: indicates the directory obtained by content.getFiledir ().
- Cache-path: indicates the directory obtained by content.getcachedir ()
- External – path: said Environment. External.getexternalstoragedirectory () to the directory.
- External files – path: said ContextCompat getExternalFilesDirs () access to the directory.
- External cache – path: said ContextCompat getExternalCacheDirs () access to the directory.
ContextCompat = getExternalFilesDirs(); ContextCompat = getExternalFilesDirs();
3) using the content: / /
Behind the configuration has been completed, you need to transfer before replaced file:// FileProvider need the content: / /, this will need to use FileProvider getUriForFile () method, the following is the full signature.
getUriForFile()
authority
android:authorities
Calling this method automatically yields a Uri object that converts file:// to content://, which we can use directly.
4) Grant temporary read and write permissions
In the configuration provider label, has an attribute android: grantUriPermissions = “true”, it said temporary permissions allow it to grant Uri.
When we generate a Uri object with content://, we cannot use it directly. We also need to grant the corresponding permission to the App that receives the Uri.
A constant of the authorization type, defined in the Intent class.
The authorization action provides two ways to authorize:
1, use the Context. GrantUriPermission () for other App awarded Uri object access.
Its full signature is as follows:
grantUriPermission()
- ToPackage: indicates the package name of the App for which permissions are granted.
- Uri: Uri for granting permission to Content ://.
- ModeFlags: Read and write permissions mentioned earlier.
In this case, the term of validity of authorization from the authorized moment, ending on equipment to restart or manually call Context. RevokeUriPermission () method, which can take back this Uri authorization.
Intent.addflags () ¶
Since this is an Intent Flag, intents provide another convenient way to authorize an Intent, using either intent.setFlags () or intent.addFlag.
This way I believe we are familiar with, I will not elaborate. With this form of authorization, the permissions end at the stack on which the App is located and are destroyed. In other words, once granted, the App has the corresponding permission to the file pointed to by the Uri for a period of time until the App is completely exited, and we cannot voluntarily revoke this permission.
Intent.addflags () does not allow the authorization to be returned. However, in most cases, the authorization can be performed in this manner. A bit suitable for employing people not doubt, doubt people do not need the truth.
Once you have the Uri of the authorized content://, you can pass the Uri to other apps via startXxx or setResult().
5) Take an example
Now that I’ve covered the basics of using FileProvider, I’ll give you a simple example to see how to use it.
Tune up the system installer to install an Apk.
3. Precautions for FileProvider
1
When configuring a provider in androidmanifest.xml, ensure that the value of Android :authorities is unique throughout the system. It is easy to understand, also saw the FileProvider. GetUriForFile (), it is found that through android: authorities attribute configuration values, to the only determined by who in response to the provider, so it need to make sure that the only within the system, Otherwise, an exception will be thrown during installation.
FileProvider.getUriForFile()
TargetSdkVersion in Lib
As mentioned earlier, if targatSdkVersion is not upgraded to 24, the previous approach is still available without the FileUriExposedException. However, if your project is distributed as a Lib Module SDK for others to use, this targetSdkVersion is not controlled by the Lib targetSdkVersion. It is the targetSdkVersion of the main project.
Therefore, if it is integrated into other apps in the form of SDK, if you need to send a File to other apps, you must adapt the FileProvider.
3. Do not use V4 package
FIleProvider exists under the Support V4 package, so to use FIleProvider you must integrate the V4 package. But for a project that doesn’t use the V4 package itself, integrating the V4 package for FileProvider increases the size of the installation package.
However, if you look closely at FileProvider, it doesn’t actually refer to any more packages, and FileProvider is essentially just a ContentProvider, so we just need to copy its code and modify it to make sure it works correctly. Instead of having to inherit the V4 package, you can use it.
Four, summary
The core of FileProvider is to improve security. It allows developers to limit the access permission of their own App files to improve security.
Therefore, in the development process, we just need to cooperate with the FileProvider to add files and directories that may be used by third-party apps to the scope of authorization, and then authorize them when sending the Intent. Other operations remain the same as before, so we will not list them here.