android kotlin java.io.FileNotFoundException:/storage/emulated/0/number.txt:打开失败:EACCES(权限被拒绝) [英] android kotlin java.io.FileNotFoundException: /storage/emulated/0/number.txt: open failed: EACCES (Permission denied)

查看:518
本文介绍了android kotlin java.io.FileNotFoundException:/storage/emulated/0/number.txt:打开失败:EACCES(权限被拒绝)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用kotlin编写适用于Android 10的应用程序. 该应用程序必须从内部存储读取名为number.txt的文件.

I'm writing an app for Android 10 using kotlin. The app has to read file named number.txt from internal storage.

但是它总是不能这样做:

But it always fails to do so:

java.io.FileNotFoundException: /storage/emulated/0/number.txt: open failed: EACCES (Permission denied)

这是清单中的内容:

...
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
...

我只需要读取文件,因此清单中没有写许可.

I have to only read the file, so there is no write permission in manifest.

这是我的代码,如您所见,我使用运行时权限:

Here is my code, as you can see I use runtime permissions:

这是请求权限的函数,如果授予了该权限,则会读取文件:

This is the function that requests permission and if it granted, reads the file:

    private fun setupPermissions() {
        val permission = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
        if (permission == PackageManager.PERMISSION_GRANTED) {
            val path = "/storage/emulated/0"
            val file = File("$path/number.txt")
            val pln = file.readText()
            plnText.text = pln
        }
        else{
            ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE), 101)
        }
    }

然后我在onCreate方法中调用setupPermissions.

就是这样,我不明白为什么它不起作用.

That's it, I don't understand why isn't it working.

谢谢.

编辑

我不知道是什么原因,但是我只是将代码更改为此:

I don't know what is the reason, but I just changed my code to this:

if (permission == PackageManager.PERMISSION_GRANTED) {
    val file = File("/storage/emulated/0/number.txt")
    val pln = file.readText()
    Log.i("IKO_APP", pln)
//  plnText.text = path.toString()
}

它可以正常工作而没有任何错误!我想知道为什么吗?

And it works without any error! I'm wondering though why?

推荐答案

对于Android 10,您可以将android:requestLegacyExternalStorage="true"添加到清单中的<application>元素中.这使您可以选择旧式存储模型,并且您的外部存储代码应该可以正常工作.

For Android 10, you can add android:requestLegacyExternalStorage="true" to your <application> element in the manifest. This opts you into the legacy storage model, and your external storage code should work.

对于读操作,您应该仍然可以照着Android 11+上的原始路径"进行操作.即使您将targetSdkVersion提高到30或更高,也可以进行更改.

And for read operations, you should be able to still do what you are doing on Android 11+, courtesy of the "raw paths" change, even after you raise your targetSdkVersion to 30 or higher.

但是,对于 write 操作,您必须在2021年下半年之前切换到其他位置:

However, for write operations, you have until the second half of 2021 to switch to something else:

  • 使用Context上的方法(例如getExternalFilesDir())访问应用程序可以写入的外部存储上的目录.您不需要任何权限即可在Android 4.4+上使用这些目录.但是,卸载应用程序后,存储在其中的数据将被删除.

  • Use methods on Context, such as getExternalFilesDir(), to get at directories on external storage into which your app can write. You do not need any permissions to use those directories on Android 4.4+. However, the data that you store there gets removed when your app is uninstalled.

使用存储访问框架,例如ACTION_OPEN_DOCUMENTACTION_CREATE_DOCUMENT.

Use the Storage Access Framework, such as ACTION_OPEN_DOCUMENT and ACTION_CREATE_DOCUMENT.

如果您的内容是媒体,则可以使用MediaStore将媒体放置在标准媒体位置.

If your content is media, you can use MediaStore to place the media in standard media locations.

这篇关于android kotlin java.io.FileNotFoundException:/storage/emulated/0/number.txt:打开失败:EACCES(权限被拒绝)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆