ArthurHub / Android-Image-Cropper

Image Cropping Library for Android, optimized for Camera / Gallery.
Apache License 2.0
6.38k stars 1.34k forks source link

how to save in my xyz folder? #852

Open sahujaunpuri opened 2 years ago

sahujaunpuri commented 2 years ago

After cropping the image, it saves it to "file:///data/user/0/package name/cache/croppedXYZ.jpg.

I want to save it in a folder named `Environment.getExternalStorageDirectory()

please guide me

RockySlayer commented 2 years ago

Hi,

Please try this:

//1. Create folder inside your app. Let's call it "folder_name": File mPhotoStorageFolder = getExternalFilesDir("folder_name");

//2. path and name for your target cropped file: String path = mPhotoStorageFolder + "/fileNmae" + ".jpg"; Uri mUriPath = Uri.parse(path);

//3. create below method for different schemes. You will use it soon: private Uri getRealUri(Uri sourceUri){ Uri mRealUri = null; if (sourceUri.getScheme() == null){ mRealUri = Uri.fromFile(new File(Objects.requireNonNull(sourceUri.getPath()))); }else{ mRealUri = sourceUri; } return mRealUri; }

//4. Then set it in the cropper's setOutputUri. You will use the above method here. CropImage.activity(imageUri) .setOutputUri(getRealUri(mUriPath)) .start(this);

//5. That's it! you can check the path on the cropped image in onActivityResult like this:

CropImage.ActivityResult result = CropImage.getActivityResult(data); String CroppedFilePath = result.getUri().getPath();

Hope this helps.