Android7.0 Open SDACRD Picture problem

Source: Internet
Author: User

1. Click on item to open a picture of the specified path via intent.

2. Test 6.0,5.0 normal operation code:

File File=new file (item.address);

Intent Intent = new Intent ("Android.intent.action.VIEW");
Intent.addcategory ("Android.intent.category.DEFAULT");

Intent.addflags (Intent.flag_activity_new_task);

Uri uri = uri.fromfile (new File (item.address));

Intent.setdataandtype (URI, "image/*");

StartActivity (Intent);

3. Run an error on 7.0.

Com.aolian.mychezai, pid:3881 android.os.FileUriExposedException:file:///storage/emulated/0/aa/17-03-01%2009-31-08 %202335-01%e7%ab%af%20b%e7%b3%bb%20stu-v-n-log.txt exposed beyond app through Intent.getdata () At Android.os.StrictMode.onFileUriExposed (strictmode.java:1816) at android.net.Uri.checkFileUriExposed (Uri.java:2350) at Android.content.Intent.prepareToLeaveProcess (Intent.java:9076) at Android.content.Intent.prepareToLeaveProcess (Intent.java:9037) at Android.app.Instrumentation.execStartActivity (Instrumentation.java:1530) at Android.app.Activity.startActivityForResult (Activity.java:4391) at Android.support.v4.app.BaseFragmentActivityJB.startActivityForResult (Basefragmentactivityjb.java:50) at Android.support.v4.app.FragmentActivity.startActivityForResult (Fragmentactivity.java:79) at Android.app.Activity.startActivityForResult (Activity.java:4335) at Android.support.v4.app.FragmentActivity.startActivityForResult (Fragmentactivity.java:859) at Android.app.Activity.startActivity (Activity.java:4697) at Android.app.Activity.startActivity (Activity.java:4665) at com.aolian.mychezai.view.filemanageractivity$7.onItemLongClick (filemanageractivity.java:377) at Android.widget.AbsListView.performLongPress (Abslistview.java:3298) at Android.widget.abslistview$checkforlongpress.run (Abslistview.java:3215) at Android.os.Handler.handleCallback (Handler.java:755)                                                                    

4. Starting with Android 7.0, when an app provides its own files for use by other apps, the app throws Fileuriexposedexception if a URI of the file://format is given. This is because Google believes that the target app may not have file permissions and can cause potential problems. So let this act quickly fail. See here. Two solutions are discussed here.

http://blog.csdn.net/xiaoyu940601/article/details/54406725

<1> Fileprovider Way

(1) Manifest statement
Declare a provider in the manifest. The name (that is, the class name) is Android.support.v4.content.FileProvider.

<manifest> ...      <application> ...         <provider            android:name= "Android.support.v4.content.FileProvider"            android:authorities = "Com.mydomain.fileprovider"            android:exported= "false"            android: Granturipermissions= "true" >            <meta-data                android:name= "Android.support.FILE_ Provider_paths "                android:resource=" @xml/file_paths "/>        </provider> ...     </application></manifest>

(2) Writing File_paths.xml

<paths xmlns:android= "Http://schemas.android.com/apk/res/android" >    <files-path name= "my_images" path = "images/"/>     ... </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.

(3) Use in Java code to share a picture as an example:

File File = ...;    // picture files to share Uri uri = fileprovider.geturiforfile (Context, "Com.mydomain.fileprovider", file);    // The second parameter is the ' authorities ' defined in manifest New Intent (intent.action_send); Intent.settype ("image/*"); Intent.putextra (Intent.extra_title, TITLE); Intent.putextra (Intent.extra_text, TEXT); Intent.addflags (intent.flag_grant_read_uri_permission);     // This step is important. Apply a temporary authorization to the target. startactivity (intent);    // or other most

2 Vmpolicy Way

The above method is certainly the recommended, correct method. But I have this problem in the actual development. Some applications (named Sina Weibo here) simply cannot understand a URI that points to the file's content://format. Once Sina Weibo receives such URIs, it cannot load the pictures and crashes when it clicks Send Weibo.
On the other hand, Sina Weibo has taken a rogue approach to the handling of rights management. It will apply for file read and write permissions at startup, and if you deny this permission, you can simply quit. I don't believe it anyway. File permissions are needed to slow down the data-store rhetoric. Slowing down the storage of data has a bunch of directories that do not require permissions to be available. But in this way, we don't really need to worry about passing a file://format URI in the past without the other party having permission.
So, how do we solve this problem? In my research, I observed a method of strict mode: StrictMode.VmPolicy.Builder.detectFileUriExposure (). As the name implies, calling this method will detect fileuriexposure. This method actually has from API18, is it possible to become the default option in API24?
Add the following code to the application.oncreate to place an unprotected vmpolicy:

if (Build.VERSION.SDK_INT >= build.version_codes. N) {    new  StrictMode.VmPolicy.Builder ();    Strictmode.setvmpolicy (Builder.build ());}

Then send the URI of the file://format directly in the old way. No more throws fileuriexposedexception.

-----

Android7.0 Open SDACRD Picture problem

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.