Android WebView localStorage

*爱你&永不变心* 提交于 2019-12-23 02:44:12

问题


I'm attempting to get a HTML5 localStorage example working within an Android WebView (Webkit, ChromeClient). However I'm having no luck. Everything works from the phones browsers just not from the WebView.

Activity:

public class BrowserActivity extends Activity {
    @Override
    public final void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        WebView browser = (WebView) findViewById(R.id.webView);

        browser.setHapticFeedbackEnabled(true);        
        browser.getSettings().setJavaScriptEnabled(true);
        browser.getSettings().setSavePassword(false);

        browser.getSettings().setDatabaseEnabled(true);
        browser.getSettings().setDomStorageEnabled(true);
        browser.getSettings().setDatabasePath("/data/data/" + browser.getContext().getPackageName() + "/databases/");

        browser.loadUrl("http://my.app.com:10099");
    }
}

Manifiest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="RestrictedBrowser"
          android:versionCode="1"
          android:versionName="1.0">
    <uses-sdk android:minSdkVersion="16"/>
    <application android:label="@string/app_name">
        <activity android:name="RestrictedBrowser.BrowserActivity"
                  android:label="@string/app_name"
                  android:theme="@android:style/Theme.Black.NoTitleBar"
                  android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
</manifest>

Error

WARN/System.err(3961): android.database.sqlite.SQLiteConstraintException: column packagename is not unique (code 19) 07-12 13:57:43.425: WARN/System.err(3961): at android.database.sqlite.SQLiteConnection.nativeExecuteForChangedRowCount(Native Method) 07-12 13:57:43.425: WARN/System.err(3961): at android.database.sqlite.SQLiteConnection.executeForChangedRowCount(SQLiteConnection.java:857) WARN/System.err(3961): at android.database.sqlite.SQLiteSession.executeForChangedRowCount(SQLiteSession.java:754) WARN/System.err(3961): at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:64) WARN/System.err(3961): at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1665) WARN/System.err(3961): at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1594) ERROR/SQLiteLog(28735): (14) cannot open file at line 30241 of [00bb9c9ce4] 07-12 13:57:47.005: ERROR/SQLiteLog(28735): (14) os_unix.c:30241: (2) open(/NotificationPermissions.db) - 07-12 DEBUG/WebKit(28735): SQLite database failed to load from /NotificationPermissions.db Cause - unable to open database file 07-12 13:57:47.005: DEBUG/WebKit(28735): [ 28735:28758 D/WebKit ] external/webkit/Source/WebCore/platform/sql/SQLiteDatabase.cpp(71) : bool WebCore::SQLiteDatabase::open(const WTF::String&, bool)


回答1:


You must create the database directory before you can use it.

Use the following code

String databasePath = browser.getContext().getDir("databases", 
                                   Context.MODE_PRIVATE).getPath(); 
browser.getSettings().setDatabasePath(databasePath); 


来源:https://stackoverflow.com/questions/17615526/android-webview-localstorage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!