Search code examples
androidrealmandroid-permissionsandroid-sdcardandroid-memory

DocumentFile.createDirectory() which will be writable as File object


I have some paths on sd card on Android 7, which are unwritable using instant access requiring, so I have to use StorageVolume.createAccessIntent and therefore use DocumentFile later instead of File. But the problem is that I need to use File, because db lib(realm), that I'm using, simply uses File object to store data. So the point is that I have to createDirectory and this directory(it's path) has to have writing access. How to do this using DocumentFile.createDirectory() or something like that?


Solution

  • So the point is that I have to createDirectory and this directory(it's path) has to have writing access

    That is not supported.

    The only direct filesystem access that you have to removable storage is via getExternalFilesDirs(), getExternalCacheDirs(), and getExternalMediaDirs() (all methods on Context). If these return 2+ items, the second and subsequent ones are locations on removable storage that are unique to your app and which you can read/write using classic File objects.

    Otherwise, keep your Realm database on external storage, then copy it to removable storage at some appropriate point (e.g., user-initiated "export" or "backup" operation).