Search code examples
androidandroid-intentandroid-gallery

How to add rotate left, right, top, bottom and crop option in gallery container


I used internal storage for saving an image and retrieve in grid view now my task is to open an image in the gallery with rotating and crop option, My sample worked in Android Lollipop version opens the image in the gallery and that contain rotate and crop option but in Android oreo version image open in gallery but no option is showed.My sample code given below to open in gallery view.

Intent intent = new Intent(Intent.ACTION_VIEW)//
                    .setDataAndType(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N ?
                                    android.support.v4.content.FileProvider.getUriForFile(MyFileActivity.this,getPackageName() + ".provider", file) : Uri.fromFile(file),
                            "image/*").addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            startActivityForResult(intent,PICK_IMAGE_REQUEST);

Solution

  • Using this Library you can crop, rotate left, right, top, bottom gallery image

    Add this library in your build.gradle(Module.app) file

    implementation 'com.theartofdev.edmodo:android-image-cropper:2.7.0'
    

    Add this line to your Proguard config file

    -keep class android.support.v7.widget.** { *; }
    

    Add CropImageActivity into your AndroidManifest.xml

    <activity android:name="com.theartofdev.edmodo.cropper.CropImageActivity"
    android:theme="@style/Base.Theme.AppCompat"/> <!-- optional (needed if 
     default theme has no action bar) -->
    

    And in onClick gallery or camera Button add following method

       CropImage.activity()                                      
      .setGuidelines(CropImageView.Guidelines.ON)
         .setAspectRatio(1,1)
        .start(YourActivityName.this);
    
        // for fragment (DO NOT use `getActivity()`)
         CropImage.activity()
         .start(getContext(), this);
    

    Now in onActivityResult method in your activity to get crop result

      @Override
      public void onActivityResult(int requestCode, int resultCode, Intent data) 
        {
        if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
         CropImage.ActivityResult result = CropImage.getActivityResult(data);
         if (resultCode == RESULT_OK) {
          Uri resultUri = result.getUri();
        } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) 
            {
           Exception error = result.getError();
           }
             }
          }