Android webview & localStorage
Solution 1
The following was missing:
settings.setDomStorageEnabled(true);
Solution 2
setDatabasePath() method was deprecated in API level 19. I advise you to use storage locale like this:
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setDatabaseEnabled(true);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
webView.getSettings().setDatabasePath("/data/data/" + webView.getContext().getPackageName() + "/databases/");
}
Solution 3
I've also had problem with data being lost after application is restarted. Adding this helped:
webView.getSettings().setDatabasePath("/data/data/" + webView.getContext().getPackageName() + "/databases/");
Solution 4
A solution that works on my Android 4.2.2, compiled with build target Android 4.4W:
WebSettings settings = webView.getSettings();
settings.setDomStorageEnabled(true);
settings.setDatabaseEnabled(true);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
File databasePath = getDatabasePath("yourDbName");
settings.setDatabasePath(databasePath.getPath());
}
Solution 5
If your app use multiple webview you will still have troubles : localStorage is not correctly shared accross all webviews.
If you want to share the same data in multiple webviews the only way is to repair it with a java database and a javascript interface.
This page on github shows how to do this.
hope this help!
Related videos on Youtube
Thomas
Updated on December 06, 2021Comments
-
Thomas over 2 years
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; } } }
-
BDL almost 3 years@Maetschl: if you want to change the code block from intentations to backticks, then at least remove the leading spaces.
-
-
danmux over 12 yearssame :) thanks a lot. I also had to make sure you target at least Android 2.1 onwards: add android:minSdkVersion="7" to your manifest and change the Project Build Target (in eclipse) to be 2.1 at least.
-
Sorin Comanescu over 10 yearsIt solved the data loss problem but what's this? Elaborate a bit?
-
iTake over 10 yearsfrom API description: "In order for the database storage API to function correctly, this method must be called with a path to which the application can write. " developer.android.com/reference/android/webkit/…
-
SBotirov almost 10 years@Ram swaroop Maybe enough just use setDomStorageEnabled (true) to enable local storage to you Maybe you use last version android and device, and this problem has not arisen at you right? (Maybe enough just use setDomStorageEnabled (true) to enable local storage to you), but some versions of android (Exm: old) problem using local storage after application restarts lost this locale storage. Someone appeared this problem and they do up vote to my answer. If you have any idea you can add another answer, too.
-
Ram Patra almost 10 yearsi tried with your solution but still the localStorage doesn't persist after the app is killed/restarted but sometimes after many restarts the localStorage resumes working and the data that had been stored there are correctly retrieved. This is unusual but it happens in my case.(Tested on Sony Xperia SP)
-
SBotirov almost 10 years@Ramswaroop I'm using Nexus4 and I also have no problem with the local storage. But when I tested on the Samsung Galaxy SII there was this problem. I'm not saying all devices are working so, just some device have it.
-
Guillaume Gendre about 8 yearsHi! we also built a tool : cobalt an open source framework for building apps with multiple webviews. We enhanced localStorage and added a pubsub plugin to "talk" between webviews. (cobaltians.org)
-
hsu.tw about 4 yearsTHX. My boss just asked the same question. This help.
-
Vahit Keskin about 2 yearsIt worked for me. Thanks for the one line code.