Android 4.0.1打破了WebView HTML 5本地存储?

MrC*_*haz 16 html5 android webview local-storage jquery-mobile

我有一个简单的html5测试页面,它使用LocalStorage来显示/保存/重新显示一段数据.

此代码在Android 2.3.x中完美运行,但在html的第18行的4.0.1中记录了一个异常,这是第一次localStorage.getItem()调用,此时JS停止了.

例外:Uncaught Error: SECURITY_ERR: DOM Exception 18 at /data/data/my.app.name/app_htmlData:18 我也尝试getCacheDir()使用相同的结果设置数据库路径.

String htmlContent = "HTML content listed below";    
File sharedDir = getActivity().getDir("htmlData", Context.MODE_PRIVATE);
WebView browser = (WebView)v.findViewById(R.id.wvBrowser);

browser.setWebChromeClient(new WebChromeClient(){
    public void onExceededDatabaseQuota(String url, String databaseIdentifier, long  currentQuota, long estimatedSize,   long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) { 
            quotaUpdater.updateQuota(estimatedSize * 2); 
        }
    });       
browser.setWebViewClient(new WebViewClient(){
    @Override
    public void onPageFinished(WebView view, String url){

        view.loadUrl("javascript:doTest()");

    });

browser.getSettings().setDatabaseEnabled(true);
browser.getSettings().setDatabasePath(sharedDir.getPath());
browser.getSettings().setDomStorageEnabled(true);
browser.loadDataWithBaseURL(mSharedDir.getPath(), 
            htmlContent, 
            "text/html", 
            "utf-8", 
            null);
Run Code Online (Sandbox Code Playgroud)

页面呈现的HTML如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Simple localStorage test</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script type="text/javascript">

        function doTest() {
            $('#stuff').append('<p>reading</p>');
            var item = read();

            $('#stuff').append('<p>writing</p>');
            localStorage['bar'] = new Date().toUTCString();

            $('#stuff').append('<p>&nbsp;</p><p>reading again</p>');
            read();
        }
        function read() {
            var item = localStorage.getItem('bar');
            if (item == null || (item == undefined)) {
                item = '';
            }
            $('#stuff').append('<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;item: ' + item + '</p>');

         return item;
        }
    </script>
</head>
<body>
    <p>-Simple localStorage test-</p>
    <div id="stuff"></div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

来源可在此处获得

MrC*_*haz 11

通过与Google工程师的一些讨论,似乎他们已经决定file:// scheme是不安全的.

解决此问题的方法是执行以下操作

browser.loadDataWithBaseURL("http://www.example.com", 
            htmlContent, 
            "text/html", 
            "utf-8", 
            null);
Run Code Online (Sandbox Code Playgroud)