无法从相机裁剪图像

时间:2016-03-28 11:43:18

标签: android camera crop

首先 - 对不起我的英语不好。 我试图从相机照片中获取作物。

这是我的openCamera方法:

private void openCamera() {

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT,
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString());
    // ******** code for crop image
    intent.putExtra("crop", "true");
    intent.putExtra("aspectX", 0);
    intent.putExtra("aspectY", 0);
    intent.putExtra("outputX", 200);
    intent.putExtra("outputY", 150);

    try {

        intent.putExtra("return-data", true);
        startActivityForResult(intent, PICK_FROM_CAMERA);

    } catch (ActivityNotFoundException e) {
    // Do nothing for now
    }
}

这是ActivityForResult:

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {

        if (requestCode == PICK_FROM_CAMERA) {
            Bundle extras = data.getExtras();
            if (extras != null) {
                photo = extras.getParcelable("data");
            }
        }
    }

但它崩溃了......

03-28 14:40:41.791 15198-15198/? E/AndroidRuntime: FATAL EXCEPTION: main
                                               java.lang.IllegalStateException: Could not execute method of the activity
                                                   at android.view.View$1.onClick(View.java:3050)
                                                   at dalvik.system.NativeStart.main(Native Method) 
                                                Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.android.camera.action.CROP dat=file:///data/data/com.android.camera/files/crop-temp (has extras) }
                                                   at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1518)
                                                   at android.app.Instrumentation.execStartActivity(Instrumentation.java:1390)
                                                   at 

绝对不知道......

1 个答案:

答案 0 :(得分:0)

使用以下代码

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", 256);    
cropIntent.putExtra("outputY", 256);    
cropIntent.putExtra("return-data", true);    
startActivityForResult(cropIntent, PIC_CROP);

OnActivityResult()方法 Bundle extras = data.getExtras(); //get the cropped bitmap Bitmap thePic = extras.getParcelable("data");