Android 12 reading file on Internal storage leads to "open failed: EACCES (Permission denied)"

Viewed 293

after hours of headache and researching (even the second site of google :) ), I decided to ask here for a solution to my problem.

What I'm trying to do is reading a Keepass File from my Internal Storage. The file is located under "/storage/emulated/0/Keepass". The file has to remain there, because it automatically syncs with a SynologyNAS. When im trying to open the file with the absolute path, I get the exception

    W/System.err: java.lang.IllegalArgumentException: The KeePass database file could not be found. You must provide a valid KeePass database file.
W/System.err:     at de.slackspace.openkeepass.KeePassDatabase.getInstance(KeePassDatabase.java:116)
W/System.err:     at de.philslr.passtick.MainActivity.readDb$lambda-5(MainActivity.kt:135)
W/System.err:     at de.philslr.passtick.MainActivity.$r8$lambda$1TiGYGtdMnuJx-_iduNC5-Ge9ag(Unknown Source:0)
W/System.err:     at de.philslr.passtick.MainActivity$$ExternalSyntheticLambda0.run(Unknown Source:2)
W/System.err:     at android.os.Handler.handleCallback(Handler.java:938)
W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:99)
W/System.err:     at android.os.Looper.loopOnce(Looper.java:201)
W/System.err:     at android.os.Looper.loop(Looper.java:288)
W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:7870)
W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
W/System.err:     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
W/System.err: Caused by: java.io.FileNotFoundException: /storage/emulated/0/Keepass/passwords_ps.kdbx: open failed: EACCES (Permission denied)
W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:575)
W/System.err:     at java.io.FileInputStream.<init>(FileInputStream.java:160)
W/System.err:     at de.slackspace.openkeepass.KeePassDatabase.getInstance(KeePassDatabase.java:113)
W/System.err:   ... 11 more
W/System.err: Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)
W/System.err:     at libcore.io.Linux.open(Native Method)
W/System.err:     at libcore.io.ForwardingOs.open(ForwardingOs.java:567)
W/System.err:     at libcore.io.BlockGuardOs.open(BlockGuardOs.java:273)
W/System.err:     at libcore.io.ForwardingOs.open(ForwardingOs.java:567)
W/System.err:     at android.app.ActivityThread$AndroidOs.open(ActivityThread.java:7756)
W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:561)
W/System.err:   ... 13 more

And the code to read the file is:

try {
            val file = File(path)
            val database = KeePassDatabase.getInstance(file).openDatabase("password")
            val entries = database.entries
            for (entry in entries) {
                Log.d("[KDBX Read]", "Entry: ${entry.title}")
            }
        } catch (e: Exception) {
            Toast.makeText(applicationContext, "Couldn't open database, there was an error", Toast.LENGTH_SHORT).show()
            e.printStackTrace()
        }

I know there were some changes, to the file management with Android 12, but I guess there has to be a way to read the file anyway.

Thanks in advance

1 Answers

The file is located under "/storage/emulated/0/Keepass".

You do not have read/write access to that location.

And the code to read the file is

It is unclear where you are getting KeePassDatabase from. If it is from this discontinued library, there is another form of getInstance() that takes an InputStream. If that is available to you, you could go with the Storage Access Framework: use ACTION_OPEN_DOCUMENT / ActivityResultContracts.OpenDocument to let the user choose the document, then use ContentResolver and openInputStream() to get an InputStream for the Uri that you get back.

Related