The source of this article is as follows http://blog.csdn.net/qq_ 27512671/article/details/71439571
When we develop functions related to [sharing files between applications], we often report this runtime exception on Android 7.0. Why do we run the code with no problem below Android 7.0 on Android 7.0 +?This is mainly from a behavior change in Android 7.0!
For Android 7.0-oriented applications, the strictmode API policy implemented by the Android framework prohibits the disclosure of file:// URI outside your application. If an intent containing a file URI leaves your application, the application fails and a fileuriexposedexception occurs. As shown in the figure:
To share files between applications, you should send a content:// URI and grant temporary access to the URI. The easiest way to do this is to use the fileprovider class.
Usage of fileprovider class:
Step 1: define a fileprovider list entry for your application. This entry can declare an XML file, which is used to specify the directory that the application can share.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapp">
<application
...>
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.myapp.fileprovider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths" />
</provider>
...
</application>
</manifest>
In this code, android:authorities Attribute should be unique. It is recommended to use [application package name + fileprovider]. It is recommended to write this android:authorities= ”${applicationId}.file_ The application package name can be found automatically.
The meta data tag specifies a path, which uses the XML file specified by resource to indicate the path:
the XML file is as follows:
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-files-path name="bga_upgrade_apk" path="upgrade_apk" />
</paths>
The way to obtain URI should also be treated differently according to the current Android system version
File dir = getExternalFilesDir("user_icon");
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
icon_path = FileProvider.getUriForFile(getApplicationContext(),
"com.mqt.android_headicon_cut.file_provider", new File(dir, TEMP_FILE_NAME));
} else {
icon_path = Uri.fromFile(new File(dir, TEMP_FILE_NAME));
}
So the problem is solved. Paste an example of installing APK adapter 7.0: http://blog.csdn.net/qq_ 27512671/article/details/70224978
Reference:
1 https://developer.android.google.cn/about/versions/nougat/android-7.0-changes.html#accessibility
https://developer.android.google.cn/training/secure-file-sharing/setup-sharing.html#DefineProvider
Read More:
- Change API level Android studio
- Android studio change package name
- The solution to the sudden change of win10 input method into traditional Chinese
- Solution to sudden change of win10 input method into traditional Chinese
- Android studio compilation error: style attribute ‘@ android:attr/windowEnterAnimation ‘not found the ultimate solution
- Unity3d mount script error “can’t add script behavior TMP”_ CoroutineTween. …”
- Android Studio error “Manifest merger failed with multiple errors, see logs” solution
- Error: Error parsing D:\new_android\Android SDK\system-images\android-25\android-wear\armeabi-v7a\de
- The solution of Android package error
- Android appears java.lang.NoClassDefFoundError A solution to the error
- Android license status unknown solution
- Android Studio | Failed to find target with hash string ‘android-26’ in: D:\Android\sdk
- Flutter development: prompt error parsing localfile when running the project: ‘/ users / xxx / Android / APP / SRC / main / androidmanifest. XML’… Solution
- The solution of “failed to load il2cpp” in Android package launched by unity
- Android compilation error unresolved reference: kotlinx solution
- Unity Android packaging error “UnityEditor.BuildPlayerWindow+BuildMethodException” solution record
- Solution to the expiration and unavailability of Android studio plug-in plugin
- Android “handler” is abstract; can’t be identified solution
- Android listview entry button click state chaos solution
- The reason and solution of Android intent value not updated