ArthurHub/Android-Image-Cropper

not Work in ANDROID 11

IMWaqasFarooq opened this issue ยท 10 comments

Its not working in android 11, the picture is not coming after capturing, same issue was raised here by others but they got a suggestion to change some code in CropActivity, but when i try to change the code , i unable to change it because that all files are read-only files not editable

๐Ÿšจ๐Ÿšจ๐Ÿšจ๐Ÿšจ๐Ÿšจ THIS LIBRARY IS NOT MAINTAINED, PLEASE READ THIS ๐Ÿšจ๐Ÿšจ๐Ÿšจ๐Ÿšจ๐Ÿšจ #838

To avoid your issues, please use the latest updated library: https://github.com/CanHub/Android-Image-Cropper

This CanHub library too is not working on my device. I'm using Poco M2 Pro (Android 11, MIUI 12). I tried with their sample code as well. Even that is not working. As soon as I choose any image from gallery or camera, it shows an error "image cropping image was cancelled by the user"

@ankiitdev open an issue in the library repository or you can even open a PR with the fix

You can try with following one .
It may help you (Only for taking picture from camera in android 29 and above)

    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getApplicationContext().getPackageManager()) != null) {
        // Create the File where the photo should go
        try {
            photoFile = getFilename(getApplicationContext());
        } catch (IOException ex) {
            ex.printStackTrace();
            // Error occurred while creating the File
        }
        if (photoFile != null) {
            picUri = FileProvider.getUriForFile(getApplicationContext(),
                    BuildConfig.APPLICATION_ID + ".provider",
                    photoFile);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, picUri);
            startActivityForResult(takePictureIntent, CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE);
        }
    }

public File getFilename(Context context) throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
String mFileName = "JPEG_" + timeStamp + "_";
File storageDir = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File mFile = File.createTempFile(mFileName, ".jpg", storageDir);
return mFile;

}

if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK && result!=null) {
Uri resultUri = result.getUri();
picUri=resultUri;
mCurrentPhotoPath = resultUri.getPath();
croppedImage.setImageURI(resultUri);

            Log.e("uploadImage", "=" + mCurrentPhotoPath);

        }
        if(resultCode==-1 && data==null)
        {

            CropImage.activity(picUri)
                    .setGuidelines(CropImageView.Guidelines.ON)
                    .setAspectRatio(1, 1) //You can skip this for free form aspect ratio)
                    .start(this);

        }
    }

Simply use above code to get image from camera and for gallery you can use regular code.
Also you need to add in manifest below code.

        <!-- resource file to create -->
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths" />
    </provider>

The issue happens here:

public static Uri getCaptureImageOutputUri(@NonNull Context context) {

It is no longer appropriate to use Uri.fromFile(file) in Android 11, we need to use FileProvider instead. Patch sample may look like this:

if (getImage != null) {
    File outputFile = new File(getImage.getPath(), "pickImageResult.jpeg");
    if (Build.VERSION.SDK_INT < 24) {
        outputFileUri = Uri.fromFile(outputFile);
    } else {
        outputFileUri = FileProvider.getUriForFile(
            context, "YOUR.APP.PACKAGE.provider", outputFile
        );
    }
}

Has anyone forked this and created a working version for SDK 30?

@donhill

please read #858

All my messages in this thread point to a forked solution and update library

Thanks. I was expecting a wrapper API that would work with existing code.
It seems that the API for this project went in a diff direction. Maybe an
adapter extension function would help folks with migration from the old
library

On Mon, Jan 10, 2022 at 8:34 PM Canato @.***> wrote:

@donhill https://github.com/donhill

please read #858
#858

โ€”
Reply to this email directly, view it on GitHub
#850 (comment),
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AABRJI5PALG4RWVS3TDVMTTUVOJK3ANCNFSM5C2YF7ZQ
.
Triage notifications on the go with GitHub Mobile for iOS
https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675
or Android
https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you were mentioned.Message ID:
@.***>

On the new project README there is a guide for migrating from one Library to the other, let me know if it is not enough and we can work into something :D.

There were some changes in need to bring this amazing lib up to date, but always open to keep improving \o/