Android 6.0系统读写文件出现FileNotFoundException:EACCES (permission denied)解决办法

先检查你的AndroidManifest.xml是否已经有读写权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
 
 
  • 1
  • 2
  • 1
  • 2

然后在获取文件流的时候,报出异常信息: 
代码:

bis = new BufferedInputStream(new FileInputStream(inFile));
 
 
  • 1
  • 1

异常是: FileNotFoundException:EACCES (permission denied) 
异常的意思是文件不存在,并且权限有问题,但是权限你也确实加了,这就是今天要解决的问题了。

由于Android 6.0 的权限是一套新的授权机制,所以在用法上也格外的不同。

参考材料: 
What Are Runtime Permissions? 
With Android 6.0 Marshmallow, Google introduced a new permission model that allows users to better understand why an application may be requesting specific permissions. Rather than the user blindly accepting all permissions at install time, the user is now prompted to accept permissions as they become necessary during application use. As you probably already know, such a change requires efforts on the part of the application developer, but what happens if you do not implement this new model? Will your application still run? What changes do you have to make to be sure your application will run smoothly on all the versions you support?

This blog will step you through a sample application that demonstrates implementing the new permissions model all while answering commonly asked questions about the new model and how it affects your application.

When to Implement the New Model 
The Good 
The big question for many is: will my existing applications still work on Marshmallow devices? The good news is the new model has backwards compatibility- i.e. it doesn’t require full support until you choose to target version 23 in your application. If you are targeting version 22 or below, your application will request all permissions at install time just as it would on any device running an OS below Marshmallow.

The Bad 
Just because we can fall back on the old model, it does not mean that we can just avoid the new model by never targeting Marshmallow. A user with a Marshmallow device will now have the ability to revoke dangerous permissions via the application settings (we will get into what a dangerous permission is later). This means that even though the user accepted all your permissions at install time, they can later decide to take some of those permissions away from you.

The Ugly 
If you have chosen not to implement the new permissions model, the revocation of permissions can cause unwanted user experiences and in some cases application crashes. The OS attempts to handle these scenarios as gracefully as possibly but developers should not rely on the OS to handle this scenario perfectly. In my experience, most permission revocations will not cause an application crash, but will absolutely degrade user experience. However, the side-effects of permission revocation are specific to an application’s implementation and I highly suggest at the very least running your application on a Marshmallow emulator and testing your application with permission revocation.

新的权限是Runtime Permissions,就是所谓的运行时权限,所以就需要在用的时候去重新请求系统权限并得到用户的允许授权。

我的解决方法是在读取文件之前加上以下代码:

int REQUEST_EXTERNAL_STORAGE = 1;
                            String[] PERMISSIONS_STORAGE = {
                                    Manifest.permission.READ_EXTERNAL_STORAGE,
                                    Manifest.permission.WRITE_EXTERNAL_STORAGE
                            };
                            int permission = ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE);

                            if (permission != PackageManager.PERMISSION_GRANTED) {
                                // We don't have permission so prompt the user
                                ActivityCompat.requestPermissions(
                                        MainActivity.this,
                                        PERMISSIONS_STORAGE,
                                        REQUEST_EXTERNAL_STORAGE
                                );
                            }
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

然后执行代码的效果是: 
这里写图片描述

点击允许,FileNotFoundException:EACCES (permission denied)问题就解决了。

有不明白的,可以直接参考下面的参考文档。

转载请注明出处,Coolspan:http://blog.csdn.net/qxs965266509

参考文档:http://www.captechconsulting.com/blogs/runtime-permissions-best-practices-and-how-to-gracefully-handle-permission-removal

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
这个错误通常在 Android 应用程序中出现,它表示应用程序没有足够的权限来访问指定的文件或目录。 在 Android 上,每个应用程序都拥有自己的沙盒环境,它们不能直接访问其他应用程序或操作系统文件系统文件。如果你的应用程序需要访问某个文件或目录,你需要在应用程序的 Manifest 文件中声明相应的权限。 例如,如果你要访问外部存储器上的文件,你需要声明以下权限: ``` <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> ``` 如果你已经声明了相应的权限,但仍然遇到 `java.io.FileNotFoundException: open failed: EACCES (Permission denied)` 错误,可能是由于以下原因导致的: 1. Android 6.0 及以上版本需要动态请求权限,你需要在运行时请求相应的权限。 2. 如果你要访问 SD 卡上的文件,你需要检查 SD 卡是否已经挂载,并且你需要检查你的应用程序是否有访问 SD 卡的权限。 3. 如果你要访问应用程序的缓存目录或其他目录,你需要检查你的应用程序是否拥有访问该目录的权限。 4. 如果你要访问应用程序之外的文件或目录,你需要检查你的应用程序是否拥有足够的权限来访问该文件或目录。 你可以在应用程序运行时,调用 `checkSelfPermission()` 方法来检查应用程序是否拥有相应的权限,如果没有,你可以调用 `requestPermissions()` 方法来请求权限。 另外,你需要确保你的文件路径是正确的,并且文件确实存在。如果文件不存在,你需要检查文件路径是否正确,并且确保你的应用程序有权限创建该文件或目录。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值