3

Trying to read values from the database, that is created in webview using javascript, but unable to get the database path using

getDatabasePath()

Which seems to be deprecated is there is any solution or work around for this.

Thanks in advance

4

1 回答 1

4
String webviewDBPath = getFilesDir().getParent() + "/";  // getFilesDir().getParent() returns base path of app private data
if (Build.VERSION.SDK_INT <= 18) { // Below kitkat
    webviewDBPath += <ur old db folder>;
}
webview.getSettings().setDatabasePath(webviewDBPath);

从 kitkat 中,我们无法更改 webview 数据库路径。默认数据库路径为 /data/data/{package name}/app_webview/databases/。对于下面的 kitkat,我们可以将数据库路径设置为任何位置。

如果要访问由 websql 创建的数据库,则从 Databases.db 中查询数据库的路径。

public String getWebViewDBPath() {
    if (Build.VERSION.SDK_INT > 18) {
        return "app_webview/databases";
    } else {
        return "{any folder}";
    }
}

public String getDBFileName(ZoomRxApp ctx, String dbName) {
    String dbFilePath = getFilesDir().getParent() + "/" +  getWebViewDBPath() + "/Databases.db";
    SQLiteDatabase webSqlDb = SQLiteDatabase.openOrCreateDatabase(dbFilePath, null);
    Cursor result = null;
    String dbFileName = null;
    try {
        String query = "select * from Databases where name like '"+dbName+"'";
        result = webSqlDb.rawQuery(query, null);
        while (result != null && result.moveToNext()) {
            String origin = result.getString(result.getColumnIndex("origin"));
            if(Build.VERSION.SDK_INT <= 18) {
                dbFileName = origin + "/" + result.getString(result.getColumnIndex("path"));
            } else {
                dbFileName = origin + "/" + result.getString(result.getColumnIndex("id"));
            }
            break;
        }
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    finally {
        if(result != null) {
            result.close();
        }
        if(webSqlDb != null) {
            webSqlDb.close();
        }
    }
}
于 2014-08-18T09:22:53.593 回答