292

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; 
        } 
    }
}
Donald Duck
  • 8,409
  • 22
  • 75
  • 99
Thomas
  • 9,299
  • 3
  • 16
  • 7
  • @Maetschl: if you want to change the code block from intentations to backticks, then at least remove the leading spaces. – BDL Jul 09 '21 at 07:54

8 Answers8

637

The following was missing:

settings.setDomStorageEnabled(true);
Thomas
  • 9,299
  • 3
  • 16
  • 7
  • 2
    same :) 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. – danmux Nov 19 '11 at 18:49
65

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/");
}
Sven Eberth
  • 3,057
  • 12
  • 24
  • 29
SBotirov
  • 13,872
  • 7
  • 59
  • 81
  • 2
    @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. – SBotirov Jun 17 '14 at 05:18
  • i 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) – Ram Patra Jun 18 '14 at 08:38
  • @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. – SBotirov Jun 18 '14 at 12:20
27

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/");
iTake
  • 4,082
  • 3
  • 33
  • 26
  • It solved the data loss problem but what's this? Elaborate a bit? – Sorin Comanescu Oct 01 '13 at 21:21
  • from 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. " http://developer.android.com/reference/android/webkit/WebSettings.html#setDatabasePath(java.lang.String) – iTake Oct 02 '13 at 06:45
16

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());
}
omfaer
  • 184
  • 3
  • 16
AngryWolf
  • 161
  • 1
  • 2
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!

Guillaume Gendre
  • 2,504
  • 28
  • 17
5

This post came up a lot recently when I was looking for a similar solution. The webview WebSettings object has deprecated database path methods since API 19 which now return blank values, meaning we can't rely on them to pull the web page storage regardless of whether we enabled it on the settings prior to loading an URL.

In order to read localStorage values from a web page, we needed to extend the WebViewClient() and override onPageFinished(), in which you can evaluate javascript on the webview as demonstrated below:

const val JAVASCRIPT_LOCAL_STORAGE_LOOKUP = "javascript:window.localStorage.getItem('KEY');"

...

override fun onPageFinished(view: WebView?, url: String?) {
    super.onPageFinished(view, url)
    view?.let { webView ->
        webView.evaluateJavascript(JAVASCRIPT_LOCAL_STORAGE_LOOKUP) { result ->
            // returns value from 'KEY'
        }
    }
}

Simply replace 'KEY' with the key of the stored object you want to access. This removes the need to provide any database implementation that may conflict with what you already have. Note that this will only poll the localStorage from the domain that the webview just finished loading. I hope this helps anyone else as it took me a bit of time to figure out.

Maetschl
  • 1,330
  • 14
  • 23
kelvin
  • 1,269
  • 1
  • 8
  • 8
2

I have a better solution for this it will work for the first time opening WebView.

you just have to use a custom callback. like this

public interface DataSavedCallback {
    void onDataSaved();
}

then use this function to get the value of the key from the webview local storage

public static void getLocalStorageAndSaveOnAndroidPref(String[] keys, WebView view, dataSavedCallback savedCallback) {
    String JAVASCRIPT_LOCAL_STORAGE_LOOKUP = "javascript:window";
    view.evaluateJavascript(JAVASCRIPT_LOCAL_STORAGE_LOOKUP.concat(".localStorage.getItem('" + keys[i] + "')"), new ValueCallback<String>() {
        @Override
        public void onReceiveValue(String value) {
            Log.d(TAG, "getLocalStorageAndSaveOnAndroidPref: " + value);
        }
    });
}

you can call the above function anywhere using your webview

myWebView.post(() -> {
    String[] keys = {"androidDeliveryAppStickyNotificationTitle","androidDeliveryAppStickyNotificationSubtitle","androidAppNewSoundAlertTitle"};
    getLocalStorageAndSaveOnAndroidPref(keys, myWebView, new dataSavedCallback() {
        @Override
        public void onDataSaved() {
            //do whatever you want
        }
    });
});
Benjamin Buch
  • 4,752
  • 7
  • 28
  • 51
Manish
  • 170
  • 9
0

if you have multiple webview, localstorage does not work correctly.
two suggestion:

  1. using java database instead webview localstorage that " @Guillaume Gendre " explained.(of course it does not work for me)
  2. local storage work like json,so values store as "key:value" .you can add your browser unique id to it's key and using normal android localstorage
Armand
  • 23,463
  • 20
  • 90
  • 119
MHP
  • 2,613
  • 1
  • 16
  • 26
  • Hi! 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. (http://cobaltians.org) – Guillaume Gendre Feb 10 '16 at 08:20