Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] -Getting File Not Found Exception when creating new File with cropped uri.getPath() #84

Closed
RanjitPati opened this issue Mar 10, 2021 · 8 comments · Fixed by #107 or #112
Closed

Comments

@RanjitPati
Copy link

RanjitPati commented Mar 10, 2021

  • Lib Version 2.2.1

Describe the bug

I am able to crop the image and put in ImageView successfully.
But when I am using the same URI path to create a file it's throwing FileNotFoundException.
The URI path is starting like content://..something/package name/ myfiles/... something like this.

When I debugged I am able to see some data like NO CACHE inside the URI value.

I am pasting my code below:
Calling Method to crop:

public void onSelectImageClick() {
   CropImage
    .activity(null)
    .setOutputCompressFormat(Bitmap.CompressFormat.JPEG)
    .setGuidelines(CropImageView.Guidelines.ON)
    .setFixAspectRatio(true)
    .start(this);
}

Getting cropped result in onActivityResuly()

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

        if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
            CropImage.ActivityResult result = CropImage.getActivityResult(data);
            if (resultCode == RESULT_OK) {

                Glide.with(this)
                        .load(result.getUri())
                        .into(actorPic);
                isImageViewAdded = true;
                //handleCropResult(CropImage.getActivityResult(data))
                imagePath = result.getUri();
                //imagePath = CropImage.getPickImageResultUri(this, data);

                //insertSingleItem(result.getUri());
            } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
                Toast.makeText(this, "Cropping failed: " + result.getError(), Toast.LENGTH_LONG).show();
            }
        }

        if(requestCode == CropImage.PICK_IMAGE_CHOOSER_REQUEST_CODE){
            Uri imageUri = CropImage.getPickImageResultUri(AddActorsActivity.this, data);
            imagePath = imageUri;
        }

    }

But the imageview is able to find the path and successfully update the cropped image inside it through Glide.

Expected behavior
I think the cropped Image is not caching and so it's happening like this.

@RanjitPati RanjitPati added the bug label Mar 10, 2021
@RanjitPati RanjitPati changed the title [BUG] -Getting File Not Found Exception when creating new File with uri.getPath() [BUG] -Getting File Not Found Exception when creating new File with cropped uri.getPath() Mar 10, 2021
@Canato
Copy link
Member

Canato commented Mar 10, 2021

Hey @RanjitPati I'm on holidays for the next weeks, but this don't look like a library issue.
The URI is returning correctly, since you can put it on the ImageView, like you said.

I cannot see the code to create the file, but maybe you can't create the file using the same URI because of the Android Scope Storage, something to check.

The bug here would be wrong URI value return, but this is working.
So not sure what the bug could be.

Open to discussions so we can solve this and help you =D

@TomasMVazquez
Copy link

Hi,
In my case I solved the problem following this:
https://developer.android.com/training/data-storage/shared/documents-files?hl=es-419

Hope it helps you too!
Regards,

@RanjitPati
Copy link
Author

I am creating the file like new File(uri.getPath()) . The same code was working before. @TomasMVazquez Can you please share the piece of code which you have used to create the file from the uri.

@Canato
Copy link
Member

Canato commented Mar 12, 2021

I am creating the file like new File(uri.getPath()) . The same code was working before. @TomasMVazquez Can you please share the piece of code which you have used to create the file from the uri.

@RanjitPati maybe this is happening because of Android OS permission changes.

Now using scope storage we don't get a file path anymore.

Maybe you can change the URI string "content" to "file"

But is not the real fix, the library will return the URI for the image using the scope storage like google force us now.

If you plan to create a file you need to get write storage permission and get the path where you put the image.

Make sense?

@AbdulmalekAlshugaa
Copy link

AbdulmalekAlshugaa commented Mar 12, 2021

com.theartofdev.edmodo:android-image-cropper:2.8.0
It works here pretty fine, you can check this library for you reference.

@TomasMVazquez
Copy link

I am creating the file like new File(uri.getPath()) . The same code was working before. @TomasMVazquez Can you please share the piece of code which you have used to create the file from the uri.

What I needed was to get Base64 from Uri/Path, originaly I was getting the path but with the change I'm using directly the Uri:

Original Code:

fun imageFileToBase64(imageFile: File): String {
    return FileInputStream(imageFile).use { inputStream ->
        ByteArrayOutputStream().use { outputStream ->
            Base64OutputStream(outputStream, Base64.DEFAULT).use { base64FilterStream ->
                inputStream.copyTo(base64FilterStream)
                base64FilterStream.flush()
                outputStream.toString()
            }
        }
    }
}

New Code:

fun imageUriToBase64(context: Context, uri: Uri): String? {
    val contentResolver = context.contentResolver
    return contentResolver.openInputStream(uri)?.use { inputStream ->
        ByteArrayOutputStream().use { outputStream ->
            Base64OutputStream(outputStream, Base64.DEFAULT).use { base64FilterStream ->
                inputStream.copyTo(base64FilterStream)
                base64FilterStream.flush()
                outputStream.toString()
            }
        }
    }
}

@Canato
Copy link
Member

Canato commented Mar 12, 2021

com.theartofdev.edmodo:android-image-cropper:2.8.0
It works here pretty fine, you can check this library for you reference.

This was the main reason I fork from the old library @AbdulmalekAlshugaa , this was the way it work before OS 11.

Now with OS 11, this behaviour changed so the library was updated, not we need to update the usage of it too.
Please let's put all discussion about this on the same place: https://github.com/CanHub/Android-Image-Cropper/discussions/87

Sadly this is an Android OS change, but if anyone know a better fix for the latest OS, using scope storage and keeping it file drop a PR cause will help everyone =)

I will close so we focus the discussion on the same place
https://github.com/CanHub/Android-Image-Cropper/discussions/87

@Canato Canato closed this as completed Mar 12, 2021
This was referenced Apr 8, 2021
@Canato
Copy link
Member

Canato commented Apr 13, 2021

Please try the latest 3.0.0 release and let me know

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants