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