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" />

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

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

异常是: 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
                                );
                            }

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

点击允许,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

猜你喜欢

转载自blog.csdn.net/qxs965266509/article/details/50606385
今日推荐