授予WRITE_EXTERNAL_STORAGE
权限后,无法写入,只有在应用程序启动正常后。我从片段请求它。在代码下方复制问题。
我正在模拟器 6.0中进行测试
/**
* Requests the WRITE_EXTERNAL_STORAGE permission.
* If the permission has been denied previously, a SnackBar will prompt the user to grant the
* permission, otherwise it is requested directly.
*/
private void requestSDCardAccessPermission() {
// BEGIN_INCLUDE(WRITE_EXTERNAL_STORAGE)
if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(),
Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
// Provide an additional rationale to the user if the permission was not granted
// and the user would benefit from additional context for the use of the permission.
// For example if the user has previously denied the permission.
Snackbar.make(mView, R.string.permission_sdcard_rationale,
Snackbar.LENGTH_INDEFINITE)
.setAction(R.string.ok, new View.OnClickListener() {
@Override
public void onClick(View view) {
requestPermissions(
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
REQUEST_WRITE_EXTERNAL_STORAGE);
}
})
.show();
} else {
// WRITE_EXTERNAL_STORAGE has not been granted yet. Request it directly.
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
REQUEST_WRITE_EXTERNAL_STORAGE);
}
// END_INCLUDE(WRITE_EXTERNAL_STORAGE)
}
onRequestPermissionsResult 回调
/**
* Callback received when a permissions request has been completed.
*/
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
if (requestCode == REQUEST_WRITE_EXTERNAL_STORAGE) {
// BEGIN_INCLUDE(permission_result)
// Received permission result for WRITE_EXTERNAL_STORAGE permission.
// Check if the only required permission has been granted
if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// WRITE_EXTERNAL_STORAGE permission has been granted, preview can be displayed
Snackbar.make(mView, R.string.sdcard_available_permission,
Snackbar.LENGTH_SHORT).show();
} else {
Snackbar.make(mView, R.string.permissions_not_granted,
Snackbar.LENGTH_SHORT).show();
}
// END_INCLUDE(permission_result)
} else {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
只要打电话
requestSDCardAccessPermission();
在清单中添加
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>;
更清楚的是,需要
WRITE_EXTERNAL_STORAGE
在运行时提供权限后重新启动应用程序。
错误日志:
Unable to decode stream: java.io.FileNotFoundException:
/storage/0E0F-380A/Pictures/2015-12-10-12-26-38-2090279097.jpg:
open failed: EACCES (Permission denied)