Android Upgrade installs APK compatible Android7.0, resolves fileuriexposedexception

Source: Internet
Author: User

We don't need to add an in-app upgrade feature when we're developing apps. When the app starts, if the latest version is detected, download the APK installation package from the server and perform the installation.
Install the APK code is generally written as follows, online anywhere can search

 Public Static void installapk (Context context, file file) {    new  Intent (intent.action_view);     = uri.fromfile (file);     " application/vnd.android.package-archive " );    Context.startactivity (intent);}

However, when we do this in the Android7.0 phone, we will find the following error log

Caused By:android.os.FileUriExposedException:file:///storage/emulated/0/android/data/net.csdn.blog.ruancoder/cache/test.apk exposed beyond app through Intent.getdata ()At android.os.StrictMode.onFileUriExposed (Strictmode.java:1799) at android.net.Uri.checkFileUriExposed (Uri.java:2346) at Android.content.Intent.prepareToLeaveProcess (Intent.java:8933) at Android.content.Intent.prepareToLeaveProcess (Intent.java:8894) at Android.app.Instrumentation.execStartActivity (Instrumentation.java:1517) at Android.app.Activity.startActivityForResult (Activity.java:4224) at Android.support.v4.app.BaseFragmentActivityJB.startActivityForResult (Basefragmentactivityjb.java: -) at Android.support.v4.app.FragmentActivity.startActivityForResult (Fragmentactivity.java: -) at Android.app.Activity.startActivityForResult (Activity.java:4183) at Android.support.v4.app.FragmentActivity.startActivityForResult (Fragmentactivity.java:859) at Android.app.Activity.startActivity (Activity.java:4507) at Android.app.Activity.startActivity (Activity.java:4475)

Let's take a look at Fileuriexposedexception official documentation

Https://developer.android.google.cn/reference/android/os/FileUriExposedException.html



Starting with Android 7.0, file://URIs are no longer allowed to be exposed to other apps in the app, otherwise the app throws Fileuriexposedexception. The reason for this is that Google believes there is some risk in using file://URIs. For example, the file is private, the file cannot be accessed by other apps, or the other app does not request Read_external_storage run-time permissions. The solution is to use Fileprovider to generate the content://URI in place of the file://URI.

Fileprovider Official documents:
Https://developer.android.google.cn/reference/android/support/v4/content/FileProvider.html
Below we use Fileprovider to resolve the above exception.
1. Declaration Fileprovider
First, declare fileprovider in the manifest file.

<manifest>    ...    <application>        ...        <provider Android:name="Android.support.v4.content.FileProvider"android:authorities="Net.csdn.blog.ruancoder.fileprovider"android:exported="false"android:granturipermissions="true"> <meta-Data Android:name="Android.support.FILE_PROVIDER_PATHS"Android:resource="@xml/file_paths"/> </provider>        ...    </application></manifest>

which
Android:name is a fixed notation.
Android:authorities can be customized to identify the unique identity of the provider, and it is recommended to combine the package name to ensure authority uniqueness.
The android:exported must be set to False, otherwise the runtime will error Java.lang.SecurityException:Provider must not be exported.
Android:granturipermissions is used to control the access rights of shared files.
The Android:resource in the <meta-data> node specifies the path to the shared file. The file_paths here is the configuration file for the directory of the provider external files, stored under res/xml/.
2. Add a File_paths.xml file
The file format is as follows

<paths>    <files-path name="name" path="path"/ >     ... </paths>

Where the root element <paths> is fixed, the inner element can be the following node:
<files-path name= "name" path= "path"/> Corresponds to Getfilesdir ().
<cache-path name= "name" path= "path"/> Corresponds to Getcachedir ().
<external-path name= "name" path= "path"/> Corresponds to Environment.getexternalstoragedirectory ().
<external-files-path name= "name" path= "path"/> Corresponds to Getexternalfilesdir ().
<external-cache-path name= "name" path= "path"/> Corresponds to Getexternalcachedir ().
Here, we store the downloaded apk file in Android/data/<package>/cache/download in SDcard, and the File_paths.xml file is as follows.

<?xml version="1.0" encoding="utf-8"?><paths>    <external-cache-path name="cache_download" path="download  "/></paths>

3. Using Fileprovider in Java code

 Public Static voidinstallapk (Context context, file file) {Intent Intent=NewIntent (Intent.action_view);    Uri data; //judge version greater than or equal to 7.0    if(Build.VERSION.SDK_INT >=Build.version_codes. N) {//"Net.csdn.blog.ruancoder.fileprovider" is the authorities configured in the manifest fileData = Fileprovider.geturiforfile (context,"Net.csdn.blog.ruancoder.fileprovider", file); //apply a temporary authorization to the targetintent.addflags (intent.flag_grant_read_uri_permission); } Else{Data=uri.fromfile (file); } intent.setdataandtype (Data,"application/vnd.android.package-archive"); Context.startactivity (intent);}

Android Upgrade installs APK compatible Android7.0, resolves fileuriexposedexception

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.