android7.0后对于file://的限制
错误信息:
04-18 14:56:58.283 4440 4440 W System.err: android.os.FileUriExposedException: file:///storage/emulated/0/temp.jpg exposed beyond app through ClipData.Item.getUri()
04-18 14:56:58.283 4440 4440 W System.err: at android.os.StrictMode.onFileUriExposed(StrictMode.java:1799)
04-18 14:56:58.283 4440 4440 W System.err: at android.net.Uri.checkFileUriExposed(Uri.java:2346)
04-18 14:56:58.283 4440 4440 W System.err: at android.content.ClipData.prepareToLeaveProcess(ClipData.java:845)
04-18 14:56:58.283 4440 4440 W System.err: at android.content.Intent.prepareToLeaveProcess(Intent.java:9044)
04-18 14:56:58.283 4440 4440 W System.err: at android.content.Intent.prepareToLeaveProcess(Intent.java:9029)
04-18 14:56:58.283 4440 4440 W System.err: at android.app.Instrumentation.execStartActivity(Instrumentation.java:1525)
04-18 14:56:58.283 4440 4440 W System.err: at android.app.Activity.startActivityForResult(Activity.java:4341)
04-18 14:56:58.283 4440 4440 W System.err: at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:48)
04-18 14:56:58.283 4440 4440 W System.err: at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:75)
04-18 14:56:58.283 4440 4440 W System.err: at android.app.Activity.startActivityForResult(Activity.java:4299)
04-18 14:56:58.283 4440 4440 W System.err: at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:856)
04-18 14:56:58.283 4440 4440 W System.err: at com.longdai.android.ui.ui2.PersonInfoActivity.b(PersonInfoActivity.java:110)
04-18 14:56:58.283 4440 4440 W System.err: at com.longdai.android.ui.ui2.PersonInfoActivity$2.onClick(PersonInfoActivity.java:388)
04-18 14:56:58.283 4440 4440 W System.err: at android.view.View.performClick(View.java:5642)
04-18 14:56:58.283 4440 4440 W System.err: at android.view.View$PerformClick.run(View.java:22489)
04-18 14:56:58.283 4440 4440 W System.err: at android.os.Handler.handleCallback(Handler.java:751)
04-18 14:56:58.283 4440 4440 W System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
04-18 14:56:58.283 4440 4440 W System.err: at android.os.Looper.loop(Looper.java:154)
04-18 14:56:58.283 4440 4440 W System.err: at android.app.ActivityThread.main(ActivityThread.java:6217)
04-18 14:56:58.283 4440 4440 W System.err: at java.lang.reflect.Method.invoke(Native Method)
04-18 14:56:58.283 4440 4440 W System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1108)
04-18 14:56:58.283 4440 4440 W System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:998)
04-18 14:56:58.323 548 635 W SurfaceFlinger: eventControl: set enabled=0
官网对于此的限制说明:
https://developer.android.com/reference/android/os/FileUriExposedException.html
The exception that is thrown when an application exposes a file:// Uri to another app.
This exposure is discouraged since the receiving app may not have access to the shared path. For example, the receiving app may not have requested the READ_EXTERNAL_STORAGE runtime permission, or the platform may be sharing the Uri across user profile boundaries.
Instead, apps should use content:// Uris so the platform can extend temporary permission for the receiving app to access the resource.
This is only thrown for applications targeting N or higher. Applications targeting earlier SDK versions are allowed to share file:// Uri, but it's strongly discouraged.
对于代码变化的说明:
在6.0的代码:
/frameworks/base/core/java/android/os/StrictMode.java
1752 /** 1753 * @hide 1754 */ 1755 public static void onFileUriExposed(String location) { 1756 final String message = "file:// Uri exposed through " + location; 1757 onVmPolicyViolation(null, new Throwable(message)); 1758 }
在7.0的代码:
/frameworks/base/core/java/android/os/StrictMode.java
1793 /** 1794 * @hide 1795 */ 1796 public static void onFileUriExposed(Uri uri, String location) { 1797 final String message = uri + " exposed beyond app through " + location; 1798 if ((sVmPolicyMask & PENALTY_DEATH_ON_FILE_URI_EXPOSURE) != 0) { 1799 throw new FileUriExposedException(message); 1800 } else { 1801 onVmPolicyViolation(null, new Throwable(message)); 1802 } 1803 }