Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Android 4.0.1 breaks WebView HTML 5 local storage?
Android 4.0.1 introduced stricter security policies that can break HTML5 local storage in WebView components. This issue primarily affects apps targeting older Android versions when loading local HTML content.
The Problem
For Android versions less than 4.4, loading data into a WebView with a file scheme as a directory won't enable local storage properly:
// This approach fails on Android 4.0.1
browser.loadDataWithBaseUrl("file:///android_asset/", html, "text/html", "UTF-8", null);
Solution 1: Add Filename to Base URL
Adding a specific filename to the base URL resolves the local storage issue on older Android versions:
// This works - include a filename in the base URL
browser.loadDataWithBaseUrl("file:///android_asset/index.html", htmlContent, "text/html", "UTF-8", null);
Solution 2: Use HTTP Base URL
Alternatively, you can use an HTTP base URL instead of the file scheme:
// Using HTTP base URL enables local storage
browser.loadDataWithBaseURL("http://localhost/", htmlContent, "text/html", "utf-8", null);
Additional WebView Configuration
For better HTML5 local storage support, configure your WebView settings:
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setDatabaseEnabled(true);
settings.setDomStorageEnabled(true);
// Set database path for older Android versions
String databasePath = getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath();
settings.setDatabasePath(databasePath);
Comparison of Solutions
| Method | Compatibility | Local Storage | Security |
|---|---|---|---|
| Directory-only base URL | Fails on Android 4.0.1 | Disabled | Restricted |
| Filename in base URL | Works on all versions | Enabled | File scheme |
| HTTP base URL | Works on all versions | Enabled | More permissive |
Conclusion
To fix HTML5 local storage issues in Android 4.0.1 WebView, include a filename in your file-based base URL or switch to an HTTP base URL. Both approaches enable proper local storage functionality across Android versions.
