2016-11-12 27 views
2

我將資產中的HTML文件加載到webview中。在牛軋糖的webview中瀏覽基於資產的html文件

html有鏈接到其他html資源文件。單擊鏈接在SDK <上正常工作24,但在單擊指向Nougat(SDK 24)設備上的第二個html頁面的鏈接時會產生FileUriExposedException錯誤。

代碼從資產的HTML加載到網頁視圖:

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]; 
     is.read(buffer); 
     is.close(); 

     return new String(buffer, "UTF-8"); 
    } 
    catch(IOException e) 
    { 
     e.printStackTrace(); 
    } 

    return ""; 
} 

的HTML是非常基本的。 的index.html

<html><a href="Page2.html">Page 2</a></html> 

Page2.html

<html><h1>Page 2</h1> 

完整的錯誤日誌:

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 exposed beyond app through Intent.getData() )但它沒有任何作用。

+0

爲什麼不使用'「file:///android_asset/Index.html」'讓該文件由WebView加載? – greenapps

+0

我認爲這是需要能夠自動加載資產的圖像,但它看起來像它的工作沒有它,但它仍然表現出相同的例外 – behelit

回答

3

我能夠通過使用shouldOverrideUrlLoading克服例外,建議由@CommonsWare here

測試模擬器上運行的牛軋糖:

web.setWebViewClient(new WebViewClient() 
    { 
     @Override 
     public boolean shouldOverrideUrlLoading(WebView view, String url) 
     { 
      Log.i("WebView", "Attempting to load URL: " + url); 

      view.loadUrl(url); 
      return true; 
     } 
    }); 
+0

我看到我的儀表板中的這次崩潰,但我不能重現這個問題模擬器(可能取決於與Intent相關的瀏覽器觸發HTML鏈接?) 但是,使用建議的解決方案時,鏈接mailto:和tel:不起作用。我可以考慮排除在WebView中加載這種類型的鏈接(並且,對於7.0以上的API,只能使用shouldOverrideUrlLoading)。 – kikoso

1

您也可以在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你所要求的網頁視圖來處理該操作。 https://developer.android.com/reference/android/webkit/WebViewClient.html#shouldOverrideUrlLoading(android.webkit.WebView,java.lang.String)

相關問題