Update believe for many people in the application is the most basic and the most common function, had been thoroughly updated function, all of a sudden test told me that sent aircraft crash on 7.0, the first reaction is that should not be ah, this function is used for a long time, how to never before heard say what’s the problem. Although I didn’t believe it, I still saw it for the first time. I had a **, and I really had a breakdown all the time, which completely broke my initial to an accidental event. At this juncture, will soon be issued version, as a result, now out of this mess, there is no way, soldiers will block the water, and then began to solve the problem. The problem is described like this:
android.os.FileUriExposedException:exposed beyond app through Intent.getData()
Check out the all-powerful Google bro, it’s the 7.0 permissions mechanism, the compatibility is really confusing sometimes. Since we found out the problem, we can solve it according to the solution. I think this is simple. Solutions are as follows:
1. Add code to androidmanifest.xml
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
Copy the code
Yes, it’s also the most searched solution on the Internet. (But you might run into a pit, more on that later)
2. Add the XML folder and provider_paths.xml file under res
<? xml version="1.0" encoding="utf-8"? > <paths> <external-path name="external_files"
path="." />
<root-path
name="root_path"
path="." />
</paths>
Copy the code
For details, please refer to Google Docs, portal (bring your own ladder).
3. The called installation code needs to be modified
File apkfile = new File(saveFileName);
if(! apkfile.exists()) {return; } Intent intent = new Intent(Intent.ACTION_VIEW); // Check whether it is AndroidN or higherif (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
Uri contentUri = FileProvider.getUriForFile(mContext, BuildConfig.APPLICATION_ID + ".versionProvider", apkfile);
intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
} else {
intent.setDataAndType(Uri.fromFile(apkfile), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
mContext.startActivity(intent);
Copy the code
Normally, this would be the end of the story, but this time, after I got all of this done, I was ready to check the results, and I actually compiled something wrong, something wrong, something wrong, you read it right, something wrong
Manifest merger failed with multiple errors, see logs
See tips, should be the resource conflict, but the tools: replace = “android: label, icon, theme, allowBackup” I set, I also checked that there are no other fileproviders in my Androidmanifest.xml. I also checked that there are no other fileproviders in the third party library. Then I annotated this code and compiled it. Just when my good patience was running out, I suddenly had a brainwave. I didn’t need the one provided by the system. I packaged it myself and modified it as follows:
public class MyFileProvider extends FileProvider {
}
Copy the code
It’s as simple as that. Then change the path in androidmanifest.xml accordingly
Uri contentUri = FileProvider.getUriForFile(mContext, BuildConfig.APPLICATION_ID + “.versionProvider”, apkfile);
Replace with:
Uri contentUri = MyFileProvider.getUriForFile(mContext, BuildConfig.APPLICATION_ID + “.versionProvider”, apkfile);
Excited to compile again…
OK, finished, no error, immediately run, trembling press the update, miracle happened, smooth installation is completed, then the bug fix is completed. I hope I can help those in need