Take and crop image with Cooliris

心已入冬 提交于 2019-12-12 04:39:07

问题


I've implemented taking picture and cropping it in my app. Usually it works perfectly. But the problem appears on devices with Cooliris gallery. The message "Gallery(process com.cooliris.media) has stopped unexpectedly" appears after the image is taken. Unfortunately, I don't have such device, and can't test it. But I have a stacktrace from a crash log.

E/AndroidRuntime(20624): Caused by: java.lang.NullPointerException
E/AndroidRuntime(20624):    at com.cooliris.media.CropImage.loadBitmap(CropImage.java:460)
E/AndroidRuntime(20624):    at com.cooliris.media.CropImage.onCreate(CropImage.java:443)
E/AndroidRuntime(20624):    at android.app.Activity.performCreate(Activity.java:4465)
E/AndroidRuntime(20624):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
E/AndroidRuntime(20624):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1932)
E/AndroidRuntime(20624):    ... 11 more

The code I use to take a photo:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
imageCaptureUri = Uri.fromFile(getTempImageFile());
intent.putExtra("return-data", true);
startActivityForResult(intent, INTENT_PICK_FROM_CAMERA);

The code to crop the image (in onActivityResult for this intent):

Intent intent = new Intent("com.android.camera.action.CROP");
intent.addCategory("android.intent.category.DEFAULT");
intent.addCategory("android.intent.category.ALTERNATIVE");
intent.addCategory("android.intent.category.SELECTED_ALTERNATIVE");
intent.setDataAndType(data.getData(), "image/jpeg");
intent.putExtra("outputX", CROPPED_IMAGE_X);
intent.putExtra("outputY", CROPPED_IMAGE_Y);
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("scale", true);
intent.putExtra("return-data", true);
startActivityForResult(intent, INTENT_CROP_FROM_CAMERA);

Seems like data.getData() is null, but I'm not sure. Does anybody know what can be the issue and how to fix it?


回答1:


I had a problem with the crop function (only cooliris media), too:

Cooliris media exception:

E/AndroidRuntime(4439): java.lang.RuntimeException: 
Unable to start activity ComponentInfo{com.cooliris.media/com.cooliris.media.CropImage}: java.lang.NullPointerException
...
E/AndroidRuntime(4439): Caused by: java.lang.NullPointerException
at com.cooliris.media.CropImage.onCreate(CropImage.java:276)

I solved the problem by giving the ACTION_IMAGE_CAPTURE intent the correct output file. This worked for me:

private static Uri tempFileUri = null;
...
String fileName = "myImg.tmp";
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
values.put(MediaStore.Images.Media.DESCRIPTION, "test description");
tempFileUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
if (tempFileUri != null) {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, tempFileUri); 
    ...
    startActivityForResult(takePictureIntent, IntentConstants.INTENT_REQUEST_CODE_TAKE_PHOTO);
}

I don't use intent.putExtra("return-data", true); anymore, because some devices don't return data (and some only small images - it seems that every device acts different). Perhaps this is the reason why the NullPointer Exception in cooliris media occurs (data sent to cooliris was null). It is really annoying that cooliris just crashes ...

The code to crop the image (in onActivityResult for this intent):

Uri picUri = null;
if (intent.getData() != null) {
    picUri = intent.getData();
} else {
    picUri = tempFileUri;
}
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", 56);
cropIntent.putExtra("outputY", 56);
cropIntent.putExtra("return-data", true);
startActivityForResult(cropIntent, INTENT_REQUEST_CODE_CROP_PHOTO);


来源:https://stackoverflow.com/questions/16893520/take-and-crop-image-with-cooliris

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!