After migrating to Android 7.0 I noticed my app was crashing with a FileUriExposedException
when loading the following url in my webView:
webView.loadUrl("file:///android_asset/myFolder/myFile.html")
I read here that this occurs because of some file system permission changes introduced in Android 7.0 where a FileUriExposedException
is now thrown when trying to share a file:// Uri in an Intent.
I tried to implement the suggested work around which was to use a FileProvider
for temporary access permissions (here), but I do not know of a way to make this work for the android_asset
folder as I only see that the available paths for the FileProvider
involve the internal and external storage paths.
From MainActivity ->
Intent intent = new Intent(view.getContext(), HelpActivity.class);
startActivity(intent);
From HelpActivity ->
webView.loadUrl("file:///android_asset/help/index.html");
Does anyone know a good way to get around this exception when needing to load a file from the assets folder in the webView?
android.os.FileUriExposedException: file:///android_asset/help/help.html exposed beyond app through Intent.getData()
at android.os.StrictMode.onFileUriExposed(StrictMode.java:1958)
at android.net.Uri.checkFileUriExposed(Uri.java:2348)
at android.content.Intent.prepareToLeaveProcess(Intent.java:9766)
at android.content.Intent.prepareToLeaveProcess(Intent.java:9720)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1609)
at android.app.Activity.startActivityForResult(Activity.java:4472)
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:4430)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:859)
at android.app.Activity.startActivity(Activity.java:4791)
at android.app.Activity.startActivity(Activity.java:4759)
at android.content.ContextWrapper.startActivity(ContextWrapper.java:366)
at org.chromium.android_webview.ResourcesContextWrapperFactory$WebViewContextWrapper.startActivity(ResourcesContextWrapperFactory.java:118)
at org.chromium.android_webview.AwContentsClient.sendBrowsingIntent(AwContentsClient.java:203)
at org.chromium.android_webview.AwContentsClient.shouldIgnoreNavigation(AwContentsClient.java:170)
at org.chromium.android_webview.AwContentsClientBridge.shouldOverrideUrlLoading(AwContentsClientBridge.java:352)
at org.chromium.base.SystemMessageHandler.nativeDoRunLoopOnce(Native Method)
at org.chromium.base.SystemMessageHandler.handleMessage(SystemMessageHandler.java:41)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Your Web content appears to contain something that is triggering the WebView
to go load help.html
via some relative path. The default behavior of a WebView
is to ask the default Web browser to load that page. That will not work for file:///android_asset/
Uri
values.
So, review the HTML, figure out what is trying to redirect to help.html
, and fix it. Also, consider adding a WebViewClient
to your WebView
, where you can control this sort of page-loading behavior via shouldOverrideUrlLoading()
.