关于 android:在 Nougat 上的 webview 中导航基于资产的 html 文件
Navigating asset based html files in webview on Nougat
我正在将一个 HTML 文件从资产加载到 web 视图中。
该 html 具有指向其他 html 资产文件的链接。单击链接在 SDK FileUriExposedException 错误。
将 html 从 assets 加载到 webview 中的代码:
wbHelp.loadDataWithBaseURL("file:///android_asset/", readAssetFileAsString("Index.html"),"text/html","UTF-8", null);
|
和 readAssetFileAsString 是:
private String readAssetFileAsString(String sourceHtmlLocation)
{ InputStream is; try { is = getContext().getAssets().open(sourceHtmlLocation); int size = is.available(); byte[] buffer = new byte[size]; return new String(buffer,"UTF-8"); return""; |
html 可以是非常基本的。
索引.html
<html>Page 2</html>
|
Page2.html
<html>Page 2
|
完整的错误日志是:
11-12 17:58:03.694 5831-5831/appname W/System.err: android.os.FileUriExposedException: file:///android_asset/Page2.html exposed beyond app through Intent.getData()
11-12 17:58:03.694 5831-5831/appname W/System.err: at android.os.StrictMode.onFileUriExposed(StrictMode.java:1799) 11-12 17:58:03.694 5831-5831/appname W/System.err: at android.net.Uri.checkFileUriExposed(Uri.java:2346) 11-12 17:58:03.694 5831-5831/appname W/System.err: at android.content.Intent.prepareToLeaveProcess(Intent.java:8933) 11-12 17:58:03.694 5831-5831/appname W/System.err: at android.content.Intent.prepareToLeaveProcess(Intent.java:8894) 11-12 17:58:03.694 5831-5831/appname W/System.err: at android.app.Instrumentation.execStartActivity(Instrumentation.java:1517) 11-12 17:58:03.694 5831-5831/appname W/System.err: at android.app.Activity.startActivityForResult(Activity.java:4224) 11-12 17:58:03.694 5831-5831/appname W/System.err: at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:48) 11-12 17:58:03.694 5831-5831/appname W/System.err: at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:77) 11-12 17:58:03.694 5831-5831/appname W/System.err: at android.app.Activity.startActivityForResult(Activity.java:4183) 11-12 17:58:03.695 5831-5831/appname W/System.err: at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:859) 11-12 17:58:03.695 5831-5831/appname W/System.err: at android.app.Activity.startActivity(Activity.java:4507) 11-12 17:58:03.695 5831-5831/appname W/System.err: at android.app.Activity.startActivity(Activity.java:4475) 11-12 17:58:03.695 5831-5831/appname W/System.err: at android.content.ContextWrapper.startActivity(ContextWrapper.java:356) 11-12 17:58:03.695 5831-5831/appname W/System.err: at org.chromium.android_webview.ResourcesContextWrapperFactory$WebViewContextWrapper.startActivity(ResourcesContextWrapperFactory.java:121) 11-12 17:58:03.695 5831-5831/appname W/System.err: at org.chromium.android_webview.AwContentsClient.sendBrowsingIntent(AwContentsClient.java:203) 11-12 17:58:03.695 5831-5831/appname W/System.err: at org.chromium.android_webview.AwContentsClient.shouldIgnoreNavigation(AwContentsClient.java:170) 11-12 17:58:03.695 5831-5831/appname W/System.err: at org.chromium.android_webview.AwContentsClientBridge.shouldOverrideUrlLoading(AwContentsClientBridge.java:256) 11-12 17:58:03.695 5831-5831/appname W/System.err: at org.chromium.base.SystemMessageHandler.nativeDoRunLoopOnce(Native Method) 11-12 17:58:03.695 5831-5831/appname W/System.err: at org.chromium.base.SystemMessageHandler.handleMessage(SystemMessageHandler.java:39) 11-12 17:58:03.695 5831-5831/appname W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102) 11-12 17:58:03.695 5831-5831/appname W/System.err: at android.os.Looper.loop(Looper.java:154) 11-12 17:58:03.695 5831-5831/appname W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6077) 11-12 17:58:03.695 5831-5831/appname W/System.err: at java.lang.reflect.Method.invoke(Native Method) 11-12 17:58:03.695 5831-5831/appname W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 11-12 17:58:03.695 5831-5831/appname W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 11-12 17:58:03.699 5831-5831/appname A/chromium: [FATAL:jni_android.cc(236)] Please include Java exception stack in crash report --------- beginning of crash 11-12 17:58:03.856 5831-5831/appname W/google-breakpad: ### ### ### ### ### ### ### ### ### ### ### ### ### 11-12 17:58:03.856 5831-5831/appname W/google-breakpad: Chrome build fingerprint: 11-12 17:58:03.856 5831-5831/appname W/google-breakpad: 1.0 11-12 17:58:03.856 5831-5831/appname W/google-breakpad: 72 11-12 17:58:03.856 5831-5831/appname W/google-breakpad: ### ### ### ### ### ### ### ### ### ### ### ### ### 11-12 17:58:03.857 5831-5831/appname A/libc: Fatal signal 6 (SIGABRT), code -6 in tid 5831 (appname) [ 11-12 17:58:03.857 270: 270 W/ ] debuggerd: handling request: pid=5831 uid=10379 gid=10379 tid=5831 |
我尝试过使用某种形式的提供程序(例如在此答案中:android.os.FileUriExposedException: file:///storage/emulated/0/test.txt 通过 Intent.getData() 暴露在应用程序之外)但它没有任何作用。
相关讨论
- 为什么不使用
"file:///android_asset/Index.html" 让 WebView 加载文件? - 我认为需要能够从资产中自动加载图像,但看起来没有它也可以工作,但它仍然表现出相同的异常
我能够通过使用 shouldOverrideUrlLoading 来克服异常,正如@CommonsWare 在这里所建议的那样:
在运行 Nougat 的模拟器上测试:
web.setWebViewClient(new WebViewClient()
{ @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { Log.i("WebView","Attempting to load URL:" + url); view.loadUrl(url); |
相关讨论
- 我确实在我的仪表板中看到了这个崩溃,但是我无法在模拟器上重现这个问题(可能取决于与 Intent 关联的浏览器来触发 HTML 链接?)但是,使用建议的解决方案,链接 mailto: 和 tel: 不起作用.我可以考虑将此类链接排除在 WebView 中加载(并且仅对 7.0 以上的 API 使用 shouldOverrideUrlLoading)。
您也可以在应用程序或活动的 onCreate 方法中添加此代码:
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build()); |
这行代码会让您的应用忽略该异常。
阅读文档,您会发现只需使用以下代码:
WebViewClient client = new WebViewClient() {
@Override public boolean shouldOverrideUrlLoading(WebView view, String url) { return false; } }; webView.setWebViewClient(client); |
也可以,因为返回 false 您要求您的 webView 处理该操作。
https://developer.android.com/reference/android/webkit/WebViewClient.html#shouldOverrideUrlLoading(android.webkit.WebView, java.lang.String)