我编写了以下代码来存储拍摄的图像照片。但由于某种原因,它适用于美国的所有 Android 操作系统,但不适用于保加利亚的牛轧糖。我无法访问保加利亚,但我的应用用户说他们无法存储图像。
Post.java
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case PermissionUtil.MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
switch (ImageUtil.userSelectionID) {
case R.id.selectPhotoProduct:
ImageUtil.showFileChooser(this);
break;
case R.id.takePhotoProduct:
if(PermissionUtil.checkPhotoPermission(this) ) {
ImageUtil.buildPicturePickerIntent(this.getPackageManager(), this);
}
break;
default:
break;
}
}
else {
// Deny code!! A message can be given.
}
break;
case PermissionUtil.MY_PERMISSIONS_REQUEST_CAMERA:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
switch (ImageUtil.userSelectionID) {
case R.id.selectPhotoProduct:
if(PermissionUtil.checkStoragePermission(this)) {
ImageUtil.showFileChooser(this);
}
break;
case R.id.takePhotoProduct:
if(PermissionUtil.checkStoragePermission(this)) {
ImageUtil.buildPicturePickerIntent(this.getPackageManager(), this);
}
break;
default:
break;
}
}
else {
// Deny code!! A message can be given.
}
break;
default:
break;
}
}
PermissionUtil.java
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public static boolean checkStoragePermission(final Context context)
{
int currentAPIVersion = Build.VERSION.SDK_INT;
if(currentAPIVersion>=android.os.Build.VERSION_CODES.M)
{
if (ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) context, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context);
alertBuilder.setCancelable(true);
alertBuilder.setTitle(context.getString(R.string.permission_necessary));
alertBuilder.setMessage(context.getString(R.string.camera_permission_necessary));
alertBuilder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions((Activity) context, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
}
});
AlertDialog alert = alertBuilder.create();
alert.show();
} else {
ActivityCompat.requestPermissions((Activity) context, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
}
return false;
} else {
return true;
}
} else {
return true;
}
}