内部存储上的Android 12阅读文件导致“打开失败:EACCE(允许拒绝)”
经过数小时的头痛和研究(甚至是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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您没有读/写入该位置的访问。
不清楚您从哪里获得
keepAssDatabase
从哪里获得。如果它来自这个停用的库 >接受inputstream
。如果您可以使用,则可以使用存储访问框架:使用action_open_document
/activityResultContracts.opendocument
让用户选择该文档,然后使用contem> content> content> contentresolver
和openInputStream()
要获得inputStream
的uri
,您可以返回。You do not have read/write access to that location.
It is unclear where you are getting
KeePassDatabase
from. If it is from this discontinued library, there is another form ofgetInstance()
that takes anInputStream
. If that is available to you, you could go with the Storage Access Framework: useACTION_OPEN_DOCUMENT
/ActivityResultContracts.OpenDocument
to let the user choose the document, then useContentResolver
andopenInputStream()
to get anInputStream
for theUri
that you get back.