如何裁剪图像并设置为imageVeiw

时间:2015-04-29 12:34:24

标签: android camera crop

我正在尝试裁剪从相机拍摄的图像并将其存储到图像视图中 - 我不确定如何定位我的裁剪图像,它似乎存储在画廊中,如果不是这样的话我就不会打扰。

问题: 我的裁剪方法可以将原始图像放在我的imageview中。

如何将裁剪后的图像指定给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);
    }
}

1 个答案:

答案 0 :(得分:1)

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

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