我已经设置:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
清单中的。但该应用程序仍然存在写入外部存储的问题。 查看应用程序管理器,可以减少应用程序的存储权限。如何启用它?为什么现在发生这种情况呢?是因为一个新的Android版本?
Settings->Applications->Application Manager-><App name>->Permissions
答案 0 :(得分:1)
尝试此操作以获得运行时权限:
if (ContextCompat.checkSelfPermission(sendNotificationActivity.this,Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED)
{
if (!ActivityCompat.shouldShowRequestPermissionRationale(sendNotificationActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE)) {
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(sendNotificationActivity.this);
alertBuilder.setCancelable(true);
alertBuilder.setTitle("Permission necessary");
alertBuilder.setMessage("Storage Permission is needed for sending Image.");
alertBuilder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
Uri.parse("package:" + activity.getPackageName()));
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
activity.startActivity(intent);
}
});
AlertDialog alert = alertBuilder.create();
alert.show();
} else {
ActivityCompat.requestPermissions(sendNotificationActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 541);
}
}
else {
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent, 2);
}
}
并覆盖此:
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
switch (requestCode) {
case 541: {
for( int i = 0; i < permissions.length; i++ ) {
if( grantResults[i] == PackageManager.PERMISSION_GRANTED ) {
Log.d("Permissions CAMERA", "Permission Granted: " + permissions[i]);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, getPhotoFileUri(photoFileName));
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent, 1);
}
} else if( grantResults[i] == PackageManager.PERMISSION_DENIED ) {
Log.d( "Permissions CAMERA", "Permission Denied: " + permissions[i] );
}
}
break;
}
default: {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
break;
}
}