Search code examples
androidandroid-gallery

Crop image after picking up from gallery android


Want to crop image after user picks image from the Gallery.

So basically when user selects image from gallery, he should be able to crop it and when he clicks ok, image should get sent to server.

On button click, I have this code which launches gallery.

Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent,RESULT_TRUE);

Then I have this code which select image from gallery and upload it to firebase.

@Override
    public  void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == RESULT_TRUE && resultCode == RESULT_OK) {
            dialog.show();
            Uri url = data.getData();
            StorageReference filepath = mstorage.child("photos").child(url.getLastPathSegment());
            filepath.putFile(url).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                    Toast.makeText(getContext(), "upload done", Toast.LENGTH_LONG).show();
                    Uri downloadurl = taskSnapshot.getDownloadUrl();
                    String imageUrl = downloadurl.toString();
                    Glide.with(getContext()).load(imageUrl).centerCrop().placeholder(R.drawable.ic_menu_camera).crossFade().into(mImage1);
                    SubscriberService subscriberService = new SubscriberService();
                    subscriberService.addItemChildList(AddImagesFragment.this, mAuth.getCurrentUser().getUid(), "images", imageUrl, place);
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }

Can someone please help me with the code to crop image.

Thanks


Solution

  • Have a look at this library , May be you will get a proper solution.

    Add uCrop to your Gradle first, than To use it write this onActivityResult,

    case IMAGE:
        UCrop.of(data.getData(), Uri.fromFile(new File(this.getCacheDir(), "IMG_" + System.currentTimeMillis())))
                        .start(YourActivity.this);
    break;
    

    After that handle uCrop Result in onActivityResults as (You can take a switch case to handle multiple requestcodes over there):

                     case UCrop.REQUEST_CROP:
                        Uri imgUri = UCrop.getOutput(data);
                        if (imgUri != null) {
                            String selectedImage = imgUri.getPath();
    
                           // load selectedImage into ImageView
                        } else {
                            Snackbar.make(view, R.string.str_bad_image_file, Snackbar.LENGTH_LONG)
                                    .show();
                        }
                        break;
                    case UCrop.RESULT_ERROR:
                        Snackbar.make(view, R.string.str_bad_image_file, Snackbar.LENGTH_LONG)
                                .show();
                        break;