Android7.0 fileuriexposedexception Problem Solving

Source: Internet
Author: User

First, the reason of fileuriexposedexception

Android7.0 does not recognize URIs beginning with file://, to convert them to content://to recognize URIs

Second, how to solve

Creation of 1.xml:

File_paths.xml the directory in which the provider external file is written: The file is placed under res/xml/. To avoid conflicts with other apps, it's best to bring your app's package name.

File contents:

<?xml version= "1.0" encoding= "Utf-8"?>
<paths>
<external-path path= "." Name= "External_storage_root"/>
</paths>

The inner element can be a files-path,cache-path,external-path,external-files-path,external-cache-path, corresponding to Context.getfilesdir (), Context.getcachedir (), Environment.getexternalstoragedirectory (), Context.getexternalfilesdir (), Context.getexternalcachedir () and several other methods. Then look at the source code found there is a not written in the document, but also can use the element, is Root-path, directly corresponding to the file system root directory. However, since it is not written into the document, there is a possibility of future removal. Use the words to pay attention to the risk.

Configuration in 2.manifests.xml

<manifest>

   ...

<application>

     ...

<provider
Android:name= "Android.support.v4.content.FileProvider"
Android:authorities= "${application_id}"
Android:exported= "false"
Android:granturipermissions= "true" >
<meta-data
Android:name= "Android.support.FILE_PROVIDER_PATHS"
Android:resource= "@xml/file_paths"/>
</provider>

...

</application>

</manifest>

3.Uri use

1) Geturiforfile Method conversion:

public static Uri Geturiforfile (context context, file file) {
Return Fileprovider.geturiforfile (Context, Gb.getcallback (). Getapplicationid (), file);
}

The second parameter is the ' authorities ' defined in manifest: Because at the time the assignment in File_paths.xml is., the second argument is: "Com.up366.mobile.fileProvider"

2) Intent plus flags

Intent.addflags (intent.flag_grant_read_uri_permission);

Note: Get local picture, no URI, no need to write

Complete.

Android7.0 fileuriexposedexception Problem Solving

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.