我有一个表单,用户可以从图库中选择图像或拍摄图像。当用户拍摄图像时它工作正常但当用户从图库中选择图像时它无法正常工作,然后我要求裁剪图像的意图。
在模拟器上裁剪来自画廊作品的图像但是在我测试过的2部手机上,当我选择裁剪应用程序时,裁剪应用程序崩溃或者如果它没有破碎,它就不起作用并显示imageView上的图像。
这是我的代码:
final int PIC_CROP = 2;
final int CAMERA_CAPTURE = 1;
final int PICK_FROM_FILE = 3;
private Uri picUri;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_CAPTURE && resultCode == RESULT_OK) {
picUri = data.getData();// get image URI
performCrop();
} else if (requestCode == PIC_CROP) {// crop and compress image
if (resultCode != RESULT_OK)
return;
Bundle extras = data.getExtras();
Bitmap thePic = extras.getParcelable("data");
FileOutputStream out = null;
ByteArrayOutputStream bao = new ByteArrayOutputStream();
double width = thePic.getWidth();
double height = thePic.getHeight();
double ratio = 650 / width;
int newheight = (int) (ratio * height);
thePic = Bitmap.createScaledBitmap(thePic, 650, newheight, true);
try {
out = new FileOutputStream(file);
thePic.compress(Bitmap.CompressFormat.JPEG, 295, out);
thePic.compress(Bitmap.CompressFormat.JPEG, 295, bao);
byte[] ba = bao.toByteArray();
switch (whichimg) {
case 1:
simg1 = Base64.encodeBytes(ba);
break;
} catch (Exception e) {
}
}
}
if (item == 0) {// take photo
Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(captureIntent, CAMERA_CAPTURE);
alert.dismiss();
} else if (item == 1) {// photo from gallery
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(
Intent.createChooser(intent, (getString(R.string.choosephototo))),
CAMERA_CAPTURE);
alert.dismiss();
}
这是裁剪代码:
private void performCrop() {
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", 356);
//cropIntent.putExtra("outputY", 256);
cropIntent.putExtra("return-data", true);
startActivityForResult(cropIntent, PIC_CROP);
} catch (ActivityNotFoundException anfe) {
MyToast.makeText(NewAdd2.this, DariGlyphUtils.reshapeText(getString(R.string.devicecouldcop)));
}
}
你可以帮帮我吗?为什么它不能正常工作?
谢谢