如何裁剪图像并设置为 imageView

How to crop an image and set to imageVeiw

我正在尝试裁剪从相机拍摄的图像并将其存储到 imageview - 我不确定如何定位我裁剪的图像,它似乎存储在画廊中,无论是否存在我都不会打扰。

问题: 我的有效裁剪方法将原始图像放入我的图像视图中。

如何将裁剪后的图像分配给图像视图?

   private static final int PICK_IMAGE = 0;
    private static final int PICK_IMAGE_FROM_GALLERY = 1;
    private static final int CROP_IMAGE = 2;
    private Uri uri;

。 . .

     btnPhotoCamera.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                Intent camera=new Intent();
                camera.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
                //camera.putExtra("crop", "true");

                File f=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

                uri = Uri.fromFile(new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"myFile.jpg"));
                camera.putExtra(MediaStore.EXTRA_OUTPUT, uri);
                startActivityForResult(camera, PICK_IMAGE);
            }
        });

然后我的裁剪方法

 private void performCrop(Uri picUri)
            {
                //NEXUS 5 OS 5 is example of this branch
                // take care of exceptions
                try {
                    // call the standard crop action intent (the user device may not
                    // support it)

                    Intent cropIntent = new Intent("com.android.camera.action.CROP");
                    // indicate image type and Uri
                    cropIntent.setDataAndType(uri, "image/*");
                    // set crop properties
                    cropIntent.putExtra("crop", "true");
                    // // indicate aspect of desired crop
                    cropIntent.putExtra("aspectX", 1);
                    cropIntent.putExtra("aspectY", 1);
                    // // // indicate output X and Y

                    // retrieve data on return
                    cropIntent.putExtra("return-data", true);
                    // start the activity - we handle returning in onActivityResult
                    startActivityForResult(cropIntent, CROP_IMAGE);
                }
                // respond to users whose devices do not support the crop action
                catch (ActivityNotFoundException anfe)
                {//NOT TESTED AS HW DOES NOT GO HERE
                    Toast toast = Toast.makeText(this,"This device doesn't support the crop action! Exception: " + anfe.toString(),Toast.LENGTH_SHORT);
                    toast.show();
                }
            }

然后我的方法来捕获触发的意图(其他应用程序)并获得它们的结果。

      protected void onActivityResult(int requestCode, int resultCode, Intent data)
        {
            // TODO Auto-generated method stub
            if (resultCode==RESULT_OK )
            {
                if(requestCode == PICK_IMAGE) //reply from camera
                {
                    performCrop(uri); //crop the picture
                }

                if(requestCode == CROP_IMAGE) //reply from crop
                {
                    Bitmap bmp = getBitmap(uri);
                    imgView.setImageBitmap(bmp);
                }
         }
    }

最后是我将 uri 转换为位图的方法 - 这就是我出错的地方,为什么这个 uri 没有被裁剪?我如何用裁剪的 uri 覆盖这个 uri?

    private Bitmap getBitmap(Uri bitmap_uri) {
        InputStream is=null;
        try
        {
            is = this.getContentResolver().openInputStream(bitmap_uri);
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
        return BitmapFactory.decodeStream(is);
    }
}

从接收到的 Intent 中获取图像,而不是从您自己的(旧的)uri 中获取图像:

//reply from crop
if(requestCode == CROP_IMAGE) {
    Bundle extras = data.getExtras();
    if (extras != null) {
        Bitmap bmp = extras.getParcelable("data");
        imgView.setImageBitmap(bmp);
    }
}