0

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);
    }
}
Fearghal
  • 10,569
  • 17
  • 55
  • 97

1 Answers1

1

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);
    }
}
Simas
  • 43,548
  • 10
  • 88
  • 116
  • great thx. bit of clarification needed - in my code, what line bundles this information so that i know what to pull on the other side; cropIntent.putExtra("return-data", true); ? So i would use Bitmap bmp = extras.getParcelable("data"); to assign the imgview?? – Fearghal Apr 29 '15 at 12:47
  • @Fearghal not sure I understand your question. performCrop, calls an external activity that returns via `onActivityResult`. That external activity sends the cropped image via the intent `data`. Just so happens that "data" is also the key with which the cropped bitmap is saved inside of the intent extras. – Simas Apr 29 '15 at 12:51
  • ohhh so i dont control the intent key name that contains the image? its alays contained int he key 'data'? – Fearghal Apr 29 '15 at 12:53
  • Ok thats it - so 'data' was the key peice if info i was missing - i thought i was meant to tell it where to put the image ina chache and then retreive it blah blah but no, simpler that tht – Fearghal Apr 29 '15 at 12:55