📜  cordova sqlite storage plugin android 11 - Shell-Bash (1)

📅  最后修改于: 2023-12-03 15:14:15.262000             🧑  作者: Mango

Cordova Sqlite Storage Plugin Android 11 - Shell-Bash

Introduction

Cordova Sqlite Storage Plugin is a plugin for Cordova mobile applications that provides a simple way to use SQLite databases. This plugin allows developers to create, read, update and delete SQLite databases from their Cordova applications.

Android 11 has introduced some restrictions on the use of SQLite databases on external storage. In order to use these databases on Android 11, developers need to use the "scoped storage" feature. This feature requires changes to existing code and usage patterns.

This tutorial will show you how to use the Cordova Sqlite Storage Plugin with Android 11 and the scoped storage feature.

Prerequisites

Before starting, you need to have the following:

  • A working Cordova project
  • Android 11 SDK installed
  • Cordova Sqlite Storage Plugin installed
Implementation
  1. Add the following code to your config.xml file:
<platform name="android">
    <config-file parent="/manifest" target="AndroidManifest.xml">
        <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    </config-file>

    <config-file parent="/manifest/application" target="AndroidManifest.xml">
        <provider android:name="cordova-sqlite-storage.SQLiteContentProvider"
            android:authorities="${applicationId}.provider"
            android:exported="false"
            android:initOrder="50"
            android:grantUriPermissions="true">
            <grant-uri-permission android:pathPattern=".*" />
        </provider>
    </config-file>
</platform>

This code adds the required permissions and the SQLite Content Provider to your AndroidManifest.xml file.

  1. Change the path of your database file from external storage to internal storage. Use the following code to create a database on internal storage.
var dbName = 'mydb.db';
var db = window.sqlitePlugin.openDatabase({
    name: dbName,
    location: 'default'
});

This code uses the default location to create the database file in internal storage.

  1. Use the plugin's isLocationEnabled() function to check if your app has been granted access to read and write files on external storage.
window.sqlitePlugin.isLocationEnabled(function(enabled) {
    console.log('Is location enabled? ' + enabled);
});

This will return a Boolean value indicating whether the app has grant to access external storage.

  1. To access files on external storage, use the requestAllFileSystems() function to request read and write access to all file systems.
window.sqlitePlugin.requestAllFileSystems(function() {
    console.log('Filesystems requested.');
});

This will prompt the user to allow your app to access external storage. If the user grants access, the function will return a success message.

Conclusion

The Cordova Sqlite Storage Plugin can be used with Android 11 and the scoped storage feature by changing the location of the database file to internal storage and requesting access to external storage using the requestAllFileSystems() function. By following these steps, you can continue to use SQLite databases in your Cordova applications on Android 11.