我希望用户从图库中选择图像,裁剪并在图像视图中进行设置。
图像被选中,Android内置的裁剪功能打开了。一切都很好,在这里。但是当我点击保存(裁剪所需的部分后)时,我没有返回到活动,但应用程序刚刚关闭而没有任何错误。
以下是我的代码:
public void changeAvatar(View view) {
Intent pickFromGallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(pickFromGallery, OpenGallery);
}
这是onActivityResult:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == OpenGallery && resultCode == RESULT_OK) {
pickedImage = data.getData();
String[] filePath = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(pickedImage, filePath, null, null, null);
cursor.moveToFirst();
cursor.close();
String imagePath = cursor.getString(cursor.getColumnIndex(filePath[0]));
thumbnial = BitmapFactory.decodeFile(imagePath);
performCrop();
// Picasso.with(this).load(imagePath).centerCrop().resize(280,240).into(pro_pic);
// pro_pic.setImageBitmap(BitmapFactory.decodeFile(imagePath));
}
if (requestCode == PIC_CROP && resultCode == RESULT_OK){
Bundle extras = data.getExtras();
thumbnial = extras.getParcelable("data");
pro_pic.setImageBitmap(thumbnial);
}
}
这是performCrop方法:
private void performCrop() {
Intent cropIntent = new Intent("com.android.camera.action.CROP");
cropIntent.setDataAndType(pickedImage, "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", 240);
//retrieve data on return
cropIntent.putExtra("return-data", true);
//start the activity - we handle returning in onActivityResult
startActivityForResult(cropIntent, PIC_CROP);
}