我们在应用尝试读取非特定于应用的文件时看到"open failed: EACCES (Permission denied)"
问题,想知道如何处理尝试使用清单文件中的android:requestLegacyExternalStorage="true"
选项,但无效。
03-13 06:44:43.169 9315 9315 W System.err:java.io.FileNotFoundException:/storage/5536-36D6/test.zip:打开失败:EACCES(权限被拒绝)03月13日06:44:43.169 9315 9315 W System.err:位于libcore.io.IoBridge.open(IoBridge.java:496)03月13日06:44:43.169 9315 9315 W System.err:位于java.io.FileInputStream。(FileInputStream.java:159)
如果您要在访问外部存储之前定位Marshmello及更高版本,则需要用户权限,您可以按照以下步骤进行操作-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission. READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
// you can read storage
}
else
{
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission. READ_EXTERNAL_STORAGE}, 411);
}
}
else
{
// you can read storage
}
根据用户操作-
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 411) {
if (grantResults.length == 0 || grantResults == null) {
// show dialog that you need access to go ahead
} else if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Your code here permission granted
} else if (grantResults[0] == PackageManager.PERMISSION_DENIED) {
// show dialog that you need access to go ahead
}
}
}