0

how to get uri from Bitmap after cropping image was selected from gallery

i tried this

public Uri getImageUri(Context inContext, Bitmap inImage) { ByteArrayOutputStream bytes = new ByteArrayOutputStream(); inImage.compress(Bitmap.CompressFormat.PNG, 100, bytes); String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null); return Uri.parse(path); } 

but uri = null

i want to get uri from this

if (resultCode == Activity.RESULT_OK && requestCode == PICK_IMAGE_REQUEST) { Uri imageUri = data.getData(); performCrop(imageUri); Log.e(TAG, "image before crop:" + imageUri); }else if(resultCode == Activity.RESULT_OK && requestCode == PIC_CROP ){ // get the returned data Bundle extras = data.getExtras(); // get the cropped bitmap Bitmap selectedBitmap = extras.getParcelable("data"); getImageUri(this,selectedBitmap); Uri ImageUri = data.getData(); Log.e(TAG, "image before crop:" + ImageUri); 

log image before crop=null

---- UPDATE CROPPING----

 private void performCrop(Uri picUri) { try { Intent cropIntent = new Intent("com.android.camera.action.CROP"); // indicate image type and Uri cropIntent.setDataAndType(picUri, "image/*"); // set crop properties here cropIntent.putExtra("crop", true); // indicate aspect of desired crop cropIntent.putExtra("aspectX", 1); cropIntent.putExtra("aspectY", 1); // indicate output X and Y cropIntent.putExtra("outputX", 128); cropIntent.putExtra("outputY", 128); // retrieve data on return cropIntent.putExtra("return-data", true); // start the activity - we handle returning in onActivityResult startActivityForResult(cropIntent, PIC_CROP); } // respond to users whose devices do not support the crop action catch (ActivityNotFoundException anfe) { // display an error message String errorMessage = "Whoops - your device doesn't support the crop action!"; Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT); toast.show(); } 

1 Answer 1

1

Im guessing you are selecting an image from your gallery.

perquisites:

  • Permissions in manifest to read/write data
  • Validation of permissions and handle no permissions granted case
  • Request code declared:

    public static final int PICK_IMAGE = 1050; 

(1) Open image selector activity

Intent intent = new Intent(); intent.setType("image/*"); intent.setAction(Intent.ACTION_GET_CONTENT); 

In Activity:

startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE); 

In Fragment:

fragment.startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE); 

(2) retrieving image from selection:

@Override public void onActivityResult(int requestCode, int resultCode, Intent data){ if(resultCode == RESULT_OK && data != null && data.getData() != null ){ switch(requestCode){ case PICK_IMAGE_REQUEST: //get filepath from the result of image selection Uri filePath = data.getData(); //Start activity for result for crop for selected image startCropActivity(filePath); break; case PIC_CROP: // get the returned data Bundle extras = data.getExtras(); // get the cropped bitmap Bitmap selectedBitmap = extras.getParcelable("data"); //do whatever with the bitmap of the image break; } } } 

Start Crop activity as so:

private void startCropActivity(Uri filePath){ try { Intent cropIntent = new Intent("com.android.camera.action.CROP"); cropIntent.setDataAndType(filePath, "image/*"); cropIntent.putExtra("crop", true); cropIntent.putExtra("aspectX", 1); cropIntent.putExtra("aspectY", 1); cropIntent.putExtra("outputX", 128); cropIntent.putExtra("outputY", 128); cropIntent.putExtra("return-data", true); startActivityForResult(cropIntent, PIC_CROP); }catch (ActivityNotFoundException anfe) { // display an error message Toast.makeText(this, "Could not crop", Toast.LENGTH_SHORT).show(); } } 
Sign up to request clarification or add additional context in comments.

4 Comments

@OsamahM I have updated my answer...the result from cropping produces a bit map of the cropped image..up to you what you want to do.. do you want to save the image? or set it as an image view ect.
@OsamahM do the logs say anything about strict mode?
i need to save image as temp and get the uri , after cropping need to add the image to the app , see here how i am adding the image to the app without cropping just selecting from galleryUri ImageUri = data.getData(); getContentResolver().takePersistableUriPermission(Objects.requireNonNull(ImageUri), Intent.FLAG_GRANT_READ_URI_PERMISSION); stickers.addSticker(ImageUri, this); finish(); startActivity(getIntent()); i need this to work also after cropping , so maybe need to save image first to get uri. @haider-malik
can you please help me fix this @haider-malik

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.