在我的应用程序中,用户可以从android中的相机或图库中选择图像。 选择图像后,用户还可以从该原始图像中裁剪图像。 但是在裁剪之后,我在HTC Desire 10 pro(Android 6.0)和Goolge Phone(Android 7.1.2)中遇到了崩溃。 我注意到这两个设备使用Google相册作为默认图库。 在三星A5(Android 6.0.1)和LG G5(Android 7.0)设备上,它工作正常。 我使用下面的代码裁剪图像:
private void getPathImage(Uri picUri) {
String picturePath = null;
Uri filePathUri = picUri;
if (picUri.getScheme().toString().compareTo("content")==0){
String[] filePathColumn = {MediaStore.Images.Media.DATA };
Cursor cursor = getApplicationContext().getContentResolver().query(picUri,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
picturePath = cursor.getString(columnIndex);
cursor.close();
}else if (picUri.getScheme().compareTo("file")==0){
picturePath = filePathUri.getEncodedPath().toString();
}
performCropImage(picturePath);
}
private void performCropImage(String picUri) {
try {
//Start Crop Activity
Intent cropIntent = new Intent("com.android.camera.action.CROP");
// indicate image type and Uri
File f = new File(picUri);
Uri contentUri = Uri.fromFile(f);
cropIntent.setDataAndType(contentUri, "image/*");
// set crop properties
cropIntent.putExtra("crop", "true");
// indicate aspect of desired crop
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
// indicate output X and Y
cropIntent.putExtra("outputX", 280);
cropIntent.putExtra("outputY", 280);
// retrieve data on return
cropIntent.putExtra("return-data", true);
// start the activity - we handle returning in onActivityResult
startActivityForResult(cropIntent, PIC_CROP);
}
// respond to users whose devices do not support the crop action
catch (ActivityNotFoundException anfe) {
// display an error message
String errorMessage = "your device doesn't support the crop action!";
Toast toast = Toast.makeText(getApplicationContext(), errorMessage, Toast.LENGTH_SHORT);
toast.show();
}
}
如何解决此问题?