在调试的时候,使用的模拟器和华为手机均正常保存。用三星的手机时,MediaStore.Images.Media.insertImage返回值为null,保存失败。
解决方案就是:检查权限是否包含了:READ_EXTERNAL_STORAGE和WRITE_EXTERNAL_STORAGE。由于我写的时候只申请了READ_EXTERNAL_STORAGE权限,没有申请WRITE_EXTERNAL_STORAGE权限,导致部分手机保存失败。
public boolean checkPermissionREAD_EXTERNAL_STORAGE() {
int currentAPIVersion = Build.VERSION.SDK_INT;
if (currentAPIVersion >= android.os.Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(app,Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED ||
ContextCompat.checkSelfPermission(app,Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) context,Manifest.permission.READ_EXTERNAL_STORAGE) ||
ActivityCompat.shouldShowRequestPermissionRationale((Activity) context,Manifest.permission.WRITE_EXTERNAL_STORAGE)) { //点击了不再提示
this.showAlertDialog("保存失败,请在<设置/权限管理>中开启存储权限后重试!");
} else {
ActivityCompat.requestPermissions(app,new String[] { Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE },MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
}
return false;
} else {
//成功
return true;
}
} else {
//成功
return true;
}
}