内部存储上的Android 12阅读文件导致“打开失败:EACCE(允许拒绝)”

发布于 2025-02-09 15:10:02 字数 2916 浏览 1 评论 0原文

经过数小时的头痛和研究(甚至是Google的第二个站点:)),我决定在这里要求解决我的问题。

我要做的是从我的内部存储中读取keatass文件。该文件位于“/storage/仿真/0/keepass”下。该文件必须保留在那里,因为它会自动与Synologynas同步。当我试图使用绝对路径打开文件时,我会得到例外

    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

和读取文件的代码:

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()
        }

我知道使用Android 12对文件管理有一些更改,但是我想必须有一种方法来无论如何,读取文件。

提前致谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

等风也等你 2025-02-16 15:10:02

该文件位于“/storage/emulation/0/keepass”下。

您没有读/写入该位置的访问。

和读取文件的代码是

不清楚您从哪里获得keepAssDatabase从哪里获得。如果它来自这个停用的库 >接受inputstream。如果您可以使用,则可以使用存储访问框架:使用action_open_document/activityResultContracts.opendocument让用户选择该文档,然后使用contem> content> content> contentresolveropenInputStream()要获得inputStreamuri,您可以返回。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文