Android中WebView加载Assets本地文件时Not allowed……的错误

Posted by baicai on August 23, 2016

Not allowed to load local resource:file:///android_asset


<p style=”margin-bottom: 1em; padding: 0px; border: 0px; font-size: 15px; clear: both; color: rgb(36, 39, 41); font-family: Arial, “Helvetica Neue”, Helvetica, sans-serif; line-height: 19.5px;”>Java:</p><pre style=”margin-bottom: 1em; padding: 5px; border: 0px; width: auto; max-height: 600px; font-family: Consolas, Menlo, Monaco, “Lucida Console”, “Liberation Mono”, “DejaVu Sans Mono”, “Bitstream Vera Sans Mono”, “Courier New”, monospace, sans-serif; word-wrap: normal; color: rgb(36, 39, 41); background-color: rgb(239, 240, 241);”><code style=”margin: 0px; border: 0px; font-size: 13px; font-family: Consolas, Menlo, Monaco, “Lucida Console”, “Liberation Mono”, “DejaVu Sans Mono”, “Bitstream Vera Sans Mono”, “Courier New”, monospace, sans-serif; white-space: inherit; background-color: rgb(239, 240, 241);”>String url =”http://192.168.84.86:8080/test/maw/js_load_test.html”; this.loadUrl(url); </code></pre><p style=”margin-bottom: 1em; padding: 0px; border: 0px; font-size: 15px; clear: both; color: rgb(36, 39, 41); font-family: Arial, “Helvetica Neue”, Helvetica, sans-serif; line-height: 19.5px;”>js_load_test.html:</p><pre style=”margin-bottom: 1em; padding: 5px; border: 0px; width: auto; max-height: 600px; font-family: Consolas, Menlo, Monaco, “Lucida Console”, “Liberation Mono”, “DejaVu Sans Mono”, “Bitstream Vera Sans Mono”, “Courier New”, monospace, sans-serif; word-wrap: normal; color: rgb(36, 39, 41); background-color: rgb(239, 240, 241);”><code style=”margin: 0px; border: 0px; font-size: 13px; font-family: Consolas, Menlo, Monaco, “Lucida Console”, “Liberation Mono”, “DejaVu Sans Mono”, “Bitstream Vera Sans Mono”, “Courier New”, monospace, sans-serif; white-space: inherit; background-color: rgb(239, 240, 241);”><script src=”file:///sdcard/test.js”></script> </code></pre><p style=”margin-bottom: 1em; padding: 0px; border: 0px; font-size: 15px; clear: both; color: rgb(36, 39, 41); font-family: Arial, “Helvetica Neue”, Helvetica, sans-serif; line-height: 19.5px;”>I’ve also tried :</p><pre style=”margin-bottom: 1em; padding: 5px; border: 0px; width: auto; max-height: 600px; font-family: Consolas, Menlo, Monaco, “Lucida Console”, “Liberation Mono”, “DejaVu Sans Mono”, “Bitstream Vera Sans Mono”, “Courier New”, monospace, sans-serif; word-wrap: normal; color: rgb(36, 39, 41); background-color: rgb(239, 240, 241);”><code style=”margin: 0px; border: 0px; font-size: 13px; font-family: Consolas, Menlo, Monaco, “Lucida Console”, “Liberation Mono”, “DejaVu Sans Mono”, “Bitstream Vera Sans Mono”, “Courier New”, monospace, sans-serif; white-space: inherit; background-color: rgb(239, 240, 241);”><script src=”file:///android_asset/www/js/test.js”></script> </code></pre><p style=”margin-bottom: 1em; padding: 0px; border: 0px; font-size: 15px; clear: both; color: rgb(36, 39, 41); font-family: Arial, “Helvetica Neue”, Helvetica, sans-serif; line-height: 19.5px;”>test.js file exists and js file path is correct. But in logcat: file:///sdcard/test.js:</p><blockquote style=”margin-bottom: 10px; padding-right: 10px; padding-left: 10px; border-width: 0px 0px 0px 2px; border-left-color: rgb(255, 235, 142); font-size: 15px; quotes: none; color: rgb(36, 39, 41); font-family: Arial, “Helvetica Neue”, Helvetica, sans-serif; line-height: 19.5px; background-color: rgb(255, 248, 220);”><p style="padding: 0px; border: 0px; clear: both;">03-18 13:01:12.467: E/Web Console(26189): Not allowed to load local resource: file:///sdcard/test.js at :0</p></blockquote><p style=”margin-bottom: 1em; padding: 0px; border: 0px; font-size: 15px; clear: both; color: rgb(36, 39, 41); font-family: Arial, “Helvetica Neue”, Helvetica, sans-serif; line-height: 19.5px;”>file:///android_asset/www/js/test.js:</p><blockquote style=”margin-bottom: 10px; padding-right: 10px; padding-left: 10px; border-width: 0px 0px 0px 2px; border-left-color: rgb(255, 235, 142); font-size: 15px; quotes: none; color: rgb(36, 39, 41); font-family: Arial, “Helvetica Neue”, Helvetica, sans-serif; line-height: 19.5px; background-color: rgb(255, 248, 220);”><p style="padding: 0px; border: 0px; clear: both;">03-18 13:01:53.467: E/Web Console(26189): Not allowed to load local resource: file:///android_asset/www/js/test.js at :0</p></blockquote><p>
</p><p>解决方法:</p><pre style=”margin-bottom: 1em; padding: 5px; border: 0px; width: auto; max-height: 600px; font-family: Consolas, Menlo, Monaco, “Lucida Console”, “Liberation Mono”, “DejaVu Sans Mono”, “Bitstream Vera Sans Mono”, “Courier New”, monospace, sans-serif; word-wrap: normal; color: rgb(36, 39, 41); background-color: rgb(239, 240, 241);”><code style=”margin: 0px; border: 0px; font-size: 13px; font-family: Consolas, Menlo, Monaco, “Lucida Console”, “Liberation Mono”, “DejaVu Sans Mono”, “Bitstream Vera Sans Mono”, “Courier New”, monospace, sans-serif; white-space: inherit; background-color: rgb(239, 240, 241);”>webview.loadDataWithBaseURL( “file:///android_asset/”, html, “text/html”, “utf-8”, null ); </code></pre>