来自相机的裁剪图像(android)

时间:2015-09-09 09:55:17

标签: android image crop

抱歉我的英文。我想用相机和裁剪图像。但有错误:

Caused by: java.lang.NullPointerException
            at com.bottom.smart.cropimage.MainActivity.onActivityResult(MainActivity.java:87)
            at android.app.Activity.dispatchActivityResult(Activity.java:4663)
            at android.app.ActivityThread.deliverResults(ActivityThread.java:3131)
            at android.app.ActivityThread.handleSendResult(ActivityThread.java:3178)
            at android.app.ActivityThread.access$1100(ActivityThread.java:134)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1277)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:154)
            at android.app.ActivityThread.main(ActivityThread.java:4624)
            at java.lang.reflect.Method.invokeNative(Native Method)

第87行:Bitmap photo = extras.getParcelable("data");

我想这样做:点击按钮,打开相机,拍照,裁剪此图像并在mainActivity中输出裁剪图像。贝娄我的代码

我的代码:

 private static final int PICK_FROM_CAMERA = 1;

    ImageView imgview;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imgview = (ImageView) findViewById(R.id.imageView1);
        Button buttonCamera = (Button) findViewById(R.id.btn_take_camera);

        buttonCamera.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

                intent.putExtra(MediaStore.EXTRA_OUTPUT,
                        MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString());

                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 (Exception e) {
                    Log.e("error", e.toString());

                }
            }
        });

    }
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == PICK_FROM_CAMERA) {
            Bundle extras = data.getExtras();
            if (data != null && data.getExtras() !=null) {
                Bitmap photo = extras.getParcelable("data");
                imgview.setImageBitmap(photo);

            }
        }


    }
}

1 个答案:

答案 0 :(得分:0)

我做过这样的事情。

Bitmap cameraBitmap = (Bitmap) data.getExtras().get("data");
SaveImageTask.SaveImageListener saveCameraImageListener = new SaveImageTask
                        .SaveImageListener() {
                    @Override
                    public void onImageSaved(String filePath) {
                        Uri uri = Uri.fromFile(new File(filePath));
                        beginCrop(uri);
                    }
                };

SaveImageTask saveTask = new SaveImageTask(saveCameraImageListener);
                saveTask.execute(cameraBitmap);

private void beginCrop(Uri source) {
    Uri outputUri = Uri.fromFile(new File(getCacheDir(), "cropped" + System.currentTimeMillis
            ()));
    new Crop(source).output(outputUri).asSquare().start(this);
}