我正在尝试实施一个在图库中选择图片后裁剪图片的活动。我实现了相机拍照,然后它显示裁剪屏幕,我可以保存结果。它显示它没有任何问题。但是当我试图在画廊中选择一张照片时。它说'无法加载'。 ' onResultActivity()'得到resultCode:0。我不知道为什么。
我的代码的主要部分是:
private void getImageFromCamera() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(profileIconFile));
startActivityForResult(intent, PICK_FROM_CAMERA);
}
private void getImageFromAlbum() {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(MediaStore.Images.Media.CONTENT_TYPE);
startActivityForResult(intent, PICK_FROM_ALBUM);
}
private Intent getCropIntent(Uri inputUri, Uri outputUri) {
Log.d(TAG, "START getCropIntent() = inputUri : " + inputUri + " | outputUri : " + outputUri);
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(inputUri, "image/*");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 200);
intent.putExtra("outputY", 200);
intent.putExtra("scale", true);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputUri);
intent.putExtra("outputFormat", Bitmap.CompressFormat.PNG.toString());
Log.d(TAG, "END getCropIntent() = intent : " + intent);
return intent;
}
private void cropImageFromCamera() {
Log.d(TAG, "START cropImageFromCamera()");
Uri uri = Uri.fromFile(profileIconFile);
Intent intent = getCropIntent(uri, uri);
startActivityForResult(intent, CROP_FROM_CAMERA);
Log.d(TAG, "END cropImageFromCamera()");
}
private void cropImageFromAlbum(Uri inputUri) {
Log.d(TAG, "START cropImageFromAlbum()");
Uri outputUri = Uri.fromFile(profileIconFile);
MyLog.d(TAG, "cropImageFromAlbum() = inputUri : " + inputUri + " | outputUri : " + outputUri);
Intent intent = getCropIntent(inputUri, outputUri);
startActivityForResult(intent, CROP_FROM_ALBUM);
Log.d(TAG, "END cropImageFromAlbum()");
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
MyLog.d(TAG, "onActivityResult() = requestCode : " + requestCode + " | resultCode : " + resultCode + " | intent : " + intent);
switch (resultCode){
case RESULT_FIRST_USER:
MyLog.d(TAG, "onActivityResult() = resultCode : 1, OK(FIRST USER)");
break;
case RESULT_OK:
MyLog.d(TAG, "onActivityResult() = resultCode : -1, OK");
break;
case RESULT_CANCELED:
MyLog.d(TAG, "onActivityResult() = resultCode : 0, CANCELED");
break;
}
switch (requestCode){
case PICK_FROM_CAMERA :
MyLog.d(TAG, "onActivityResult() = PICK_FROM_CAMERA");
cropImageFromCamera();
break;
case PICK_FROM_ALBUM :
MyLog.d(TAG, "onActivityResult() = PICK_FROM_ALBUM > intent is " + intent);
if(intent != null) {
Uri dataUri = intent.getData();
MyLog.d(TAG, "onActivityResult() = PICK_FROM_ALBUM > dataUri is " + dataUri);
if (dataUri != null) {
cropImageFromAlbum(dataUri);
}
}
break;
case CROP_FROM_CAMERA :
MyLog.d(TAG, "onActivityResult() = CROP_FROM_CAMERA > profileIconFile : " + profileIconFile + " | profileIconImage : " + profileIconImage );
Picasso.with(this).load(profileIconFile).into(profileIconImage);
uploadProfileIcon();
break;
case CROP_FROM_ALBUM :
MyLog.d(TAG, "onActivityResult() = CROP_FROM_ALBUM > profileIconFile : " + profileIconFile + " | profileIconImage : " + profileIconImage );
if(intent != null){
Picasso.with(this).load(profileIconFile).into(profileIconImage);
uploadProfileIcon();
}
break;
}
}
private void uploadProfileIcon() {
Log.d(TAG, "START uploadProfileIcon() = memberInfoItem.seq : " + memberInfoItem.seq + " | profileIconFile : " + profileIconFile);
RemoteLib.getInstance().uploadMemberIcon(memberInfoItem.seq, profileIconFile);
memberInfoItem.memberIconFilename = profileIconFilename + ".png";
Log.d(TAG, "END uploadProfileIcon() = memberInfoItem.seq : " + memberInfoItem.memberIconFilename);
}

答案 0 :(得分:1)
尝试添加cropIntent.putExtra("return-data", true)
并检查。
private void performCrop(Uri picUri) {
try {
Intent cropIntent = new Intent("com.android.camera.action.CROP");
cropIntent.setDataAndType(picUri, "image/*");
cropIntent.putExtra("crop", true);
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
cropIntent.putExtra("outputX", 128);
cropIntent.putExtra("outputY", 128);
cropIntent.putExtra("return-data", true);
startActivityForResult(cropIntent, PIC_CROP);
}
catch (ActivityNotFoundException e) {
String errorMessage = "Whoops - your device doesn't support the crop action!";
Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PIC_CROP) {
if (data != null) {
Bundle extras = data.getExtras();
Bitmap bitmap = extras.getParcelable("data");
imgView.setImageBitmap(bitmap);
}
}
}