Android webview & localStorage

Thomas picture Thomas · May 5, 2011 · Viewed 133.1k times · Source

I have a problem with a webview which may access to the localStorage by an HTML5 app. The test.html file informs me that local storage is'nt supported by my browser (ie. the webview). If you have any suggestion..

package com.test.HelloWebView; 
import android.app.Activity; 
import android.content.Context; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.KeyEvent; 
import android.webkit.WebChromeClient; 
import android.webkit.WebSettings; 
import android.webkit.WebStorage; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 
public class HelloWebView extends Activity { 
WebView webview; 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    webview = (WebView) findViewById(R.id.webview); 
    webview.getSettings().setJavaScriptEnabled(true); 
    webview.setWebViewClient(new HelloWebViewClient()); 
    webview.loadUrl("file:///android_asset/test.html"); 
    WebSettings settings = webview.getSettings(); 
    settings.setJavaScriptEnabled(true); 
    settings.setDatabaseEnabled(true); 
    String databasePath = this.getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath(); 
    settings.setDatabasePath(databasePath);
    webview.setWebChromeClient(new WebChromeClient() { 
    public void onExceededDatabaseQuota(String url, String databaseIdentifier, long currentQuota, long estimatedSize, long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) { 
            quotaUpdater.updateQuota(5 * 1024 * 1024); 
        } 
    }); 
} 
public boolean onKeyDown(int keyCode, KeyEvent event) { 
    if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) { 
        webview.goBack(); 
        return true; 
    } 
    return super.onKeyDown(keyCode, event); 
} 
private class HelloWebViewClient extends WebViewClient { 
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
        view.loadUrl(url); 
        return true; 
    } 
}
} 

Answer

Thomas picture Thomas · May 9, 2011

The following was missing:

settings.setDomStorageEnabled(true);