objectbox-java icon indicating copy to clipboard operation
objectbox-java copied to clipboard

Cannot access data.mdb after reinstall apps (Kotlin)

Open 51M0Ng opened this issue 1 year ago • 3 comments

Describe the bug Hello guys, I am testing is it possible to do a full Objectbox backup to google drive via 3rd sync apps Then when I developing I found out the daba.mdb I was created on the /storage/emulated/0/Documents/ (Public Documents Directory) after uninstall and reinstall the debug apk. it turns into no permission to access. I think the android is notice the debug apk is another apps and identified as not a file owner.

val dataDir = File("${Environment.getExternalStorageDirectory()}/Documents") MyObjectBox.builder().androidContext(context.applicationContext).baseDirectory(dataDir).build()

The Exception io.objectbox.exception.DbException: Could not open database environment; please check options and file system (13: Permission denied) (error code 13)

Then I do a lot of googling, and i find out this GitHub - Could not open database environment; please check options and file system #12 , Then i did below action:

I was tried add .filemode(666) to see is it possible to allow all user to read and write the data.mdb val dataDir = File("${Environment.getExternalStorageDirectory()}/Documents") MyObjectBox.builder().filemode(666).androidContext(context.applicationContext).baseDirectory(dataDir).build()

Turns out it throws java.lang.IllegalArgumentException: File mode may not include any executable flags

Honestly I don't know which means do need including executable flags or not? As Official API Doc, should no need to add executable flag.

Then I tried add .filemode(6) val dataDir = File("${Environment.getExternalStorageDirectory()}/Documents") MyObjectBox.builder().filemode(6).androidContext(context.applicationContext).baseDirectory(dataDir).build()

It not throw any errors but nothing change, still not accessible after reinstall debug apk. And i checking and comfirm the data.mdb is permission -rw-rw---- (0660) (** With or without .filemode(6))

After another round of googling, I found out Stackoverflow - Java Android create file chmod 666 & Android Developer - Access to app-specific directories on external storage

As mentioned, look like Android 11+ would not allowed to access file that create from other apps that is not shared permission.

And I am not sure is it a policy change that required ObjectBox need to be react to.

Conclusions:

  1. Can I still able to access the data.mdb after reinstall the same debug Apk install same device.
  2. Can I able implementing Objectbox database backup to google drive, then I can download it to specific directory and let objectbox access it again?

Basic info:

  • ObjectBox version: 3.2.1
  • Reproducibility: Always
  • Device: Nokia 8.3 5G
  • OS: Android 12

To Reproduce Steps to reproduce the behavior:

  1. Initial ObjectBox and locate data.mdb to /storage/emulated/0/Documents/
  2. Uninstall Debug APK
  3. Install Debug APK via android studio
  4. Throw 13: Permission denied exception

Additional Info: I tried this how to allow another app to access my app's data directory?

51M0Ng avatar Jul 23 '22 10:07 51M0Ng

First of all: yes, ObjectBox can re-open a database file it previously created.

However, how you get the database file to a directory accessible from your app is up to you. Note that since Android 11 it is not longer possible to access files from other apps stored on external storage. I suggest to write code that copies the database file to your app's internal storage directory. E.g. by adding a file picker using the Android storage access framework.

greenrobot-team avatar Jul 25 '22 07:07 greenrobot-team

Got your ideas, cause I simply initialize the database from builder, I just thought the create process was done but library code only for sure. And yes, I didn't consider copying a file bit by bit to external storage and making it all accessible. That's a great idea.

But honestly, for my point of view if it was controllable from the library itself instead of making a work around would be much nicer, anyway, thank you so much. cheers.

51M0Ng avatar Jul 25 '22 11:07 51M0Ng

@51M0Ng If your code can provide a File to an existing database file, there is an initialDbFile(File) option when building the store. It will copy the given file for you.

greenrobot-team avatar Jul 26 '22 06:07 greenrobot-team

Without additional information, we are unfortunately not sure how to resolve this issue. Therefore this issue has been automatically closed. Feel free to comment with additional details and we can re-open this issue.

github-actions[bot] avatar Aug 17 '22 01:08 github-actions[bot]