PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, July 27, 2022

[FIXED] How to pick an image from gallery and save within the app after cropping?

 July 27, 2022     android, crop, java, uri     No comments   

Issue

I am new to Android development and I tried my best to pick an image from gallery and saving within the app after cropping but I failed. Please help me to solve this issue. I tried to mix up different codes but nothing is working for me.

if (resultCode == RESULT_OK) {
    //Uri photoUri = data.getData();
    //if (photoUri != null) {

    // photoPickerIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, uri);
    CropImage.activity(android.net.Uri.parse(data.getDataString()))
            .setAspectRatio(1,1)
            .setFixAspectRatio(true)
            .start(activity);
            CropImage.ActivityResult result1 = CropImage.getActivityResult(data);
    if (resultCode == RESULT_OK) {
        iv.setImageURI(result1.getUri());
    } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
        Exception error = result1.getError();
        Log.d(TAG, "onActivityResult: " + error.getMessage());
    }
            //currentImage = MediaStore.Images.Media.getBitmap(this.getContentResolver(), photoUri);
            //selectedImage.setImageBitmap(currentImage);

   // }

Solution

First of All Add a dependency inside your project Gradle.built(app:odle) file

Like

dependencies {
implementaion 'com.theartofdev.edmodo:android-image-cropper:2.8.+'
}

After that add following two permission inside manifiest file

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Create a Constant inside your class private static final int REQUEST_FOR_GALLARY = 1; you will need this .

After that Put the following Code inised your button on which u click to open gallery or whatever your are using .

Intent gallaryIntent = new Intent();
        gallaryIntent.setAction(Intent.ACTION_GET_CONTENT);
        gallaryIntent.setType("image/*");
        startActivityForResult(gallaryIntent, REQUEST_FOR_GALLARY);

After that Override the medthod onActivityResult of your Activity class Like

 @Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent 
             data) {
                  super.onActivityResult(requestCode, resultCode, data);

         if (requestCode == REQUEST_FOR_GALLARY && resultCode == RESULT_OK && data != 
              null) {

                 Uri imageUri = data.getData();

         CropImage.activity()
                .setGuidelines(CropImageView.Guidelines.ON)
                .start(this);
    }
    if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
        CropImage.ActivityResult result = CropImage.getActivityResult(data);

        if (resultCode == RESULT_OK) {


            Uri resultUri = result.getUri();

           //Save image wherever you want to save it 
        }
    }
}

Now Change your Code According to your Recuirement and Save image whereever you want to store .



Answered By - Rashid Kalhoro
Answer Checked By - Candace Johnson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing