Android adaptation – FileProvider
Before because of
In response to Google’s call, the App is available on Android 8.0. Only the situations encountered in our App are recorded.
I refer to the FileProvider documentation and other resources that can be searched on the Web.
Adapter core
The core of this adaptation is the use of FileProvider.
AndroidManifest adaptation
Add a provider node to the Application node.
<manifest>.<application>.<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
</appliction>
</manifest>
Copy the code
The above writing method is basically fixed, many items can be modified, but it is not recommended to do, the following is just a record.
Item 1 can be modified
android:name="androidx.core.content.FileProvider"
Copy the code
The above is the Provider class that specifies the service. The above is the Android AndroidX package provided by the class, can be customized. You can use a ContentProvider to provide the goal of the service.
Item 2 can be modified
android:authorities="${applicationId}.fileProvider"
Copy the code
A unique string can be used. The above is the commonly used writing method.
Item 3 can be modified
android:name="android.support.FILE_PROVIDER_PATHS"
Copy the code
Yes, you can change it, but if you don’t implement the Provider yourself, this value can only be this. This string is defined in the FileProvider class.
Item 4 can be modified
android:resource="@xml/file_paths"
Copy the code
Defines the configuration file used by the FileProvider class. The file name is optional as long as it does not violate the naming convention.
Add a specific ContentProvider, FileProvider, package name androidx.core.content.
FileProvider ADAPTS files
File address res/ XML /file_paths.xml
The contents of the empty file are as follows
<?xml version="1.0" encoding="utf-8"? >
<paths>
</paths>
Copy the code
Android Studio prompts for these five places.
<files-path name="name" path="path" />
<cache-path name="name" path="path" />
<external-path name="name" path="path" />
<external-files-path name="name" path="path" />
<external-cache-path name="name" path="path" />
Copy the code
There’s another one on the website
<! -- this directory is only available on API 21+ devices. -->
<external-media-path name="name" path="path" />
Copy the code
You can see another one in the code
<! -- The root directory of this definition is / -->
<root-path name="name" path="path" />
Copy the code
The difference between the preceding -paths is that the root directory is defined differently.
-path |
The root directory |
---|---|
files-path | Context.getFilesDir() |
cache-path | Context.getCacheDir() |
external-path | Environment.getExternalStorageDirectory() |
external-files-path | Context.getExternalFilesDir(null) |
external-cache-path | Context.getExternalCacheDir() |
external-media-path | Context.getExternalMediaDirs() |
root-path | / |
The name attribute in the Path node
Unique not to repeat
The Path property of the Path node
The name of a folder that automatically includes subdirectories under the folder
Example:
<external-path name="pic" path="Pictures" />
Copy the code
After this definition, all files in /sdcard/Pictures can be sent from your App to other apps for use.
The specific application
Our App doesn’t cover that much. These are simple applications.
Photo sharing
Scene, App internally generated a picture, and use the system to share.
The core code
Uri uri = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
uri = FileProvider.getUriForFile(this, getPackageName() + ".fileProvider", file);
} else {
uri = Uri.fromFile(file);
}
Copy the code
Video playback
There is a feature for video playback using the system player, while there is asynchronous video download. If the download is complete, play the local video. Will use this feature.
The core code
Uri uri = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
uri = FileProvider.getUriForFile(this, getPackageName() + ".fileProvider", file);
} else{ uri = Uri.fromFile(file); } // The above is the same as the image sharing. Intent.setflags (intent.flag_grant_read_uri_permission); intent.setflags (intent.flag_grant_read_uri_permission);Copy the code
conclusion
This adaptation is relatively simple. I think there are two things to note.
- I want to revise everything. I can’t leave anything out. This includes code and configuration
- Version of the judgment