在AndroidManifest.xml里加入
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />//这里有错的话那就在name后面加入 tools:ignore="ProtectedPermissions"
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
在你需要请求读取内存权限的activity.class里加入
@Override
protected void onResume() {
super.onResume();
checkPermission();
}
private void checkPermission(){
if (ContextCompat.checkSelfPermission(this, WRITE_EXTERNAL_STORAGE)!=PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this, new String[] { WRITE_EXTERNAL_STORAGE }, REQUEST_PERMISSION_OK);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
// super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_PERMISSION_OK) {
if (grantResults.length >= 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "存储权限已开通", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "存储权限被拒绝", Toast.LENGTH_SHORT).show();
}
}
}