Fearghal
Fearghal

Reputation: 11427

How to crop an image and set to imageVeiw

Im trying to crop an image taken from a camera and store it to an imageview - i m not sure how target my cropped image, it seems to be stored in gallery which im not bothered if it is or not.

Problem: my crop method which works puts the original image in my imageview.

How can i assign the cropped image to the 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);
            }
        });

Then my crop method

 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();
                }
            }

then my method to catch fired intents (other apps) and get their results.

      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);
                }
         }
    }

and finally my method that converts the uri to bitmap - this is where im going wrong, why is this uri not cropped? How can i overwrite this uri witht he cropped one?

    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);
    }
}

Upvotes: 0

Views: 1000

Answers (1)

Simas
Simas

Reputation: 44158

Fetch the image from the Intent received, not from your own (old) uri:

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

Upvotes: 1

Related Questions