从外部存储读取文件
Reading file from External Storage
Docs 说:
In order to read or write files on the external storage, your app must
acquire the READ_EXTERNAL_STORAGE or WRITE_EXTERNAL_STORAGE system
permissions.
我没有在我的应用程序清单中使用 READ_EXTERNAL_STORAGE
或 WRITE_EXTERNAL_STORAGE
权限。但是很奇怪,因为这段代码运行成功:
File file = new File("/storage/sdcard0/ImageStore/Mat1.jpg");
FileInputStream fis = null;
byte[] buffer = null;
try {
fis = new FileInputStream(file);
buffer = new byte[1024];
fis.read(buffer );
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
为什么在拒绝读写权限的情况下这段代码运行无异常?
注:
如果外部存储没有安装、安装错误或...,那么显然会发生 FileNotFoundException
developer.android.com 就 READ_EXTERNAL_STORAGE 权限
声明了这一点
This permission is enforced starting in API level 19. Before API level 19, this permission is not enforced and all apps still have access to read from external storage.
READ_EXTERNAL_STORAGE
在 API 级别 16 中引入并在 Api 级别 19 (KITKAT) 中强制执行。在此之前,没有从外部存储读取的特殊权限,只能向其中写入。来自文档
This permission is enforced starting in API level 19. Before API level
19, this permission is not enforced and all apps still have access to
read from external storage.
Docs 说:
In order to read or write files on the external storage, your app must acquire the READ_EXTERNAL_STORAGE or WRITE_EXTERNAL_STORAGE system permissions.
我没有在我的应用程序清单中使用 READ_EXTERNAL_STORAGE
或 WRITE_EXTERNAL_STORAGE
权限。但是很奇怪,因为这段代码运行成功:
File file = new File("/storage/sdcard0/ImageStore/Mat1.jpg");
FileInputStream fis = null;
byte[] buffer = null;
try {
fis = new FileInputStream(file);
buffer = new byte[1024];
fis.read(buffer );
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
为什么在拒绝读写权限的情况下这段代码运行无异常?
注:
如果外部存储没有安装、安装错误或...,那么显然会发生 FileNotFoundException
developer.android.com 就 READ_EXTERNAL_STORAGE 权限
声明了这一点This permission is enforced starting in API level 19. Before API level 19, this permission is not enforced and all apps still have access to read from external storage.
READ_EXTERNAL_STORAGE
在 API 级别 16 中引入并在 Api 级别 19 (KITKAT) 中强制执行。在此之前,没有从外部存储读取的特殊权限,只能向其中写入。来自文档
This permission is enforced starting in API level 19. Before API level 19, this permission is not enforced and all apps still have access to read from external storage.