简体   繁体   中英

How can Large Database to be store in external file directory in android

I have created an application which is already published on playstore but I am facing the large database issue. I am using the backup feature where I am storing the data in File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS) + File.separator + "backup");

Here user's database is getting larger and because of that DB file is not working properly. So what should I do.

Maximum Database Size 140 tb but it will depends on your device disk size.

I suspect it is ~2 gigabytes (that might be due to 32-bit architecture, although certain programs come with largefile support, allowing more that that). NO need to worry about db size.

android 10 or below or api level below 29 user legeacy external stoarge

application
        android:largeHeap="true"
        android:icon="@mipmap/ic_launcher"
        android:name=".MyApplication"
        android:label="demoapp"
        android:requestLegacyExternalStorage="true">

Caution : The external storage might become unavailable if the user removes the SD card or connects the device to a computer. And the files are still visible to the user and other apps that have the READ_EXTERNAL_STORAGE permission. So if your app's functionality depends on these files or you need to completely restrict access, you should instead write your files to the internal storage.

Request external storage permissions: To write to the public external storage, you must request the WRITE_EXTERNAL_STORAGE permission in your manifest file:

<manifest ...>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

  <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />   ...
</manifest>

Verify that external storage is available: Because the external storage might be unavailable — such as when the user has mounted the storage to a PC or has removed the SD card that provides the external storage — you should always verify that the volume is available before accessing it.

For example, the following methods are useful to determine the storage availability:

/* Checks if external storage is available for read and write */
public boolean isExternalStorageWritable() {
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        return true;
    }
    return false;
}
/* Checks if external storage is available to at least read */
public boolean isExternalStorageReadable() {
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state) ||
        Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        return true;
    }
    return false;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM