2

I developed an Android application via cordova which running inside a webview. How can I make the app use persistent storage in chromium? It only consumes temporary storage. The reason I need to use persistent storage is that I can request more spaces. If I use temporary storage and it runs of out maximum limit, the temporary storage will remove old data on the database.

I run below code in chrome console and it gives me we are using 0 of 0 bytes which means there is 0 space in persistent storage I can use.

navigator.webkitPersistentStorage.queryUsageAndQuota (
    function(usedBytes, grantedBytes) {
        console.log('we are using ', usedBytes, ' of ', grantedBytes, 'bytes');
    },
    function(e) { console.log('Error', e);  }
);

Then I run below code to request more storage:

navigator.webkitPersistentStorage.requestQuota (
    100 * 1024 * 1024, function(grantedBytes) {
        window.requestFileSystem(PERSISTENT, grantedBytes, (d) => console.log(d), e => console.error(e));

    }, function(e) { console.log('Error', e); }
);

after that nothing happens and it is still 0 storage space I can use. I wonder how I can use persistent storage in webview. Do I need to specify any permission for that?

The permission for my app is:

<uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.GET_TASKS" />
    <uses-permission android:name="android.permission.NFC" />
    <uses-permission android:name="android.permission.INTERNET" />

I have checked another question Android webview & localStorage but it is quite different with mine. I am asking to grant persistent storage permission for my webview but that question doesn't explain how to use persistent storage instead it uses temporary storage which is the default storage in webview.

I have set setDatabaseEnabled(true) on java code but it doesn't use persistent storage. In stead, it uses temporary storage.

Joey Yi Zhao
  • 37,514
  • 71
  • 268
  • 523

1 Answers1

0

setDatabaseEnabled() seems to be the only method still available:

Sets whether the database storage API is enabled. The default value is false.

even if one cannot change the location of the database anymore; as setDataBasePath() reads:

This method was deprecated in API level 19.

Database paths are managed by the implementation and calling this method will have no effect.

Using JavaScriptInterface might be a possible workaround for persisting data; for example.

The Cordova documentation also lists some possible options, in comparison.

Community
  • 1
  • 1
Martin Zeitler
  • 1
  • 19
  • 155
  • 216
  • I have set `setDatabaseEnabled(true) ` on java code but it doesn't use `persistent storage`. In stead, it uses `temporary storage`. – Joey Yi Zhao Apr 01 '19 at 00:17
  • @ZhaoYi if it doesn't work directly with the `WebView` (Cordova is just a custom version), using SQLite as storage backend might be the best option - this at least persists until the app had been uninstalled... the default location for that database is internal storage (works without requesting run-time permissions). – Martin Zeitler Apr 01 '19 at 00:50
  • I don't have problem on persisting the data on local storage. The problem is the storage is used by webview is temporary which means it will remove data if it runs out of the maximum storage limit. And I can't claim more temporary storage. I don't mind using either SQLite or IndexDB as long as they can use persistent storage. – Joey Yi Zhao Apr 01 '19 at 00:55
  • @ZhaoYi those SQLite plugins use almost the same storage facility as most Android apps use and also work with iOS & Windows... still, some JS code would need to be migrated, but queries are quicker and there is no limit in database size; except that the cursor window might be limited to at most 2mb (at least on Android this might apply, unsure about the other platforms). – Martin Zeitler Apr 01 '19 at 01:29
  • The limit size is not determined by any database, like SQLite or IndexedDB. It is limited by browser. Checkout here: https://developer.chrome.com/apps/offline_storage – Joey Yi Zhao Apr 01 '19 at 03:14
  • @ZhaoYi for `LocalStorage`, it's +/- 5mb overall and it even might be dropped, as you describe it...the Cordova `WebView` is not exactly the same as `Chrome`; only [CustomTabs](https://developer.chrome.com/multidevice/android/customtabs) is the common `Chrome`... for a simple key/value store, a simple table with two columns already suffices. – Martin Zeitler Apr 01 '19 at 06:20
  • So there is no way to make indexedDB to use persistent storage on webview? – Joey Yi Zhao Apr 01 '19 at 08:40