当我将领域数据库本地存储在文件夹中时,我的应用程序在 android 11 崩溃?

My App is crashing on android 11 when I store realm database locally in a folder?

当我要在本地内部存储中保存领域数据库时,应用程序在 Android 11 崩溃。该应用程序适用于所有其他版本,但今天我将我的应用程序更新为 android 11,但它崩溃了。

E/AndroidRuntime: FATAL EXCEPTION: main
Process:  PID: 7882
io.realm.exceptions.RealmError: Unrecoverable error. open() failed: Operation not permitted in io_realm_internal_Group.cpp line 210
    at io.realm.internal.Group.nativeWriteToFile(Native Method)
    
   
      private Realm realm;

      try {

        File file = new File(folderPath);
        if (!file.exists()) {
            file.mkdir();
        }
      
        realm = new File(file, fileName);
       
        if (realm .exists()) {
            realm .delete();
        }
       
        realm.writeCopyTo(realm ); // crashing here in this line

    } catch (IOException e) {
        e.printStackTrace();
      
    }

存储权限在 android11 中完全改变了应用程序如何访问它。现在应用程序无法访问除它们自己的数据目录之外的其他存储。某些不访问文件系统就无法运行的应用程序(如文件管理器)需要具有所有文件访问权限。 您的应用程序在 android 11 崩溃,很可能是因为它没有写入存储的权限。 如需更多信息 - Read here

阅读文档后,我通过将 getExternalStorageDirectory 替换为 context.getExternalFilesDir() 解决了这个问题;将领域数据库本地保存在文件夹中以备备份。

See Doc