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

Keeping original photo taken from camera results in blank photo. #142

Closed
raylee4204 opened this issue Jul 5, 2016 · 9 comments
Closed

Comments

@raylee4204
Copy link

When you take a photo from camera, and try tapping checkmark icon without making any changes (ratio set to original) to the photo passed to UCropActivity, it results in a blank photo.

@raylee4204 raylee4204 changed the title Keeping original photo take from camera results in blank photo. Keeping original photo taken from camera results in blank photo. Jul 6, 2016
@shliama
Copy link
Contributor

shliama commented Jul 7, 2016

I cannot reproduce that.
Can you post here a snippet of you camera/ucrop flow & attach image file from camera too.

@raylee4204
Copy link
Author

raylee4204 commented Jul 7, 2016

Basically, I do the following to launch camera intent

     // Create an image file name
    String timeStamp = DateTimeFormat.forPattern("yyyyMMdd_HHmmss").print(LocalDateTime.now());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = new File(context.getFilesDir(), "images");

    //Make sure directory exists
    if (!storageDir.exists()) {
        storageDir.mkdir();
    }

    File newFile = null;
    try {
        newFile = File.createTempFile(
                imageFileName,  /* prefix */
                ".jpg",         /* suffix */
                storageDir      /* directory */
        );
    } catch (IOException e) {
        LogUtils.e("Create Image File", e.getMessage());
    }

    if (newFile == null) {
        newFile = new File(storageDir, imageFileName + ".jpg");
    }

    DisplayUtils.dispatchCameraIntent(this, newFile, REQUEST_IMAGE_CAPTURE);
public static boolean dispatchCameraIntent(Activity activity, File file, int requestCode) {
        Intent takePictureIntent = DisplayUtils.createCameraIntent(activity);
        // Ensure that there's a camera activity to handle the intent
        if (takePictureIntent != null) {
            // Save a file: path for use with ACTION_VIEW intents
            Uri uri = FileProvider.getUriForFile(activity, IMAGE_FILE_PROVIDER, file);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
            List<ResolveInfo> resInfoList = activity.getPackageManager()
                    .queryIntentActivities(takePictureIntent, PackageManager.MATCH_DEFAULT_ONLY);
            for (ResolveInfo resolveInfo : resInfoList) {
                String packageName = resolveInfo.activityInfo.packageName;
                activity.grantUriPermission(packageName, uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
            }
            activity.startActivityForResult(takePictureIntent, requestCode);
            return true;
        } else {
            return false;
        }
    }

Then, in onActivityResult for REQUEST_IMAGE_CAPTURE

if (resultCode == RESULT_OK) {
        Uri selectedImage = Uri.fromFile(newFile);

        Uri croppedImage = Uri.fromFile(croppedImageFile);

        UCrop.Options options = new UCrop.Options();
        options.setActiveWidgetColor(ContextCompat.getColor(context, R.color.color_accent));
        options.setAllowedGestures(UCropActivity.SCALE, UCropActivity.ALL, UCropActivity.SCALE);
        options.setCompressionQuality(80);
        options.setMaxBitmapSize(1200);
        options.setToolbarColor(ContextCompat.getColor(context, R.color.color_primary));
        options.setStatusBarColor(ContextCompat.getColor(context, R.color.color_primary_dark));
        UCrop.of(selectedImage, croppedImage).withOptions(options)
                            .start(this);
}

@krokyze
Copy link

krokyze commented Jul 19, 2016

This error happens not only with a photo taken from the camera. For example, in the sample app, I choose to crop a random photo, don't touch any settings, go straight to done and I get this.

Problem is in BitmapLoadTask class when:

final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options);
if (options.outWidth == -1 || options.outHeight == -1) {
     return new BitmapWorkerResult(new IllegalArgumentException("Bounds for bitmap could not be retrieved from the Uri: [" + mInputUri + "]"));
}

@raylee4204
Copy link
Author

So what's the solution for this?

@shliama
Copy link
Contributor

shliama commented Jul 20, 2016

I'm looking into the issue, but I have limited amount of time currently. This task has high priority and will be fixed in the next library update.

@PGMacDesign
Copy link
Contributor

Hey guys, ran into the same issue myself.
Just to reiterate what others have said, it can be reproduced for any image (from camera, from gallery, or downloaded into a file) when the aspect ratio being saved is identical to the aspect ratio of the image.
I'll fork a branch tonight and see if I can help out at all. Any particular branch I should pull from to minimize merge conflicts if I fix it?
-PGMacDesign

@PGMacDesign
Copy link
Contributor

PGMacDesign commented Jul 29, 2016

An update:
Still looking through the code to try and fix it. If anyone needs an image to test with to recreate the issue, I uploaded this one to imgur which has the correct dimensions to reproduce and crash the app. To replicate, just use this photo, start the crop, and chance nothing. On mine, it was maintaining the original, but I have also seen it on a 1,1 aspect ratio being sent in manually too.
http://i.imgur.com/mNglKRx.jpg

Furthermore, when running the SampleActivity in the cloned repo, the error it is coming up with is:

07-29 09:21:58.445 13174-13174/com.yalantis.ucrop.sample E/TransformImageView: onFailure: setImageUri
java.lang.IllegalArgumentException: Bounds for bitmap could not be retrieved from the Uri: [file:///data/user/0/com.yalantis.ucrop.sample/cache/SampleCropImage.jpg]
at com.yalantis.ucrop.task.BitmapLoadTask.doInBackground(BitmapLoadTask.java:113)
at com.yalantis.ucrop.task.BitmapLoadTask.doInBackground(BitmapLoadTask.java:41)
at android.os.AsyncTask$2.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)

PGMacDesign added a commit to PGMacDesign/uCrop that referenced this issue Jul 29, 2016
As per issue 142 (Yalantis#142), it mentions the issue of files being sent back as null if they make no changes (No cropping). The solution was simply just to check the paths and confirm if they are identical. In the event that the paths are the same, trying to copy one file to the other will cause both files to become null. simply skipping this step if the paths are identical solves that problem. This pull request should fix that issue entirely
@PGMacDesign
Copy link
Contributor

Ok! Got it fixed. It was an issue with File copying. Short version, sending the same Uri as both input and output + no cropping caused the code to try to overwrite the other and both ended up being null. This resulted in both files being returned as null and the original getting wiped out.
My pull request should solve that problem and shouldn't cause issues with cropped photos.

shliama added a commit that referenced this issue Sep 8, 2016
Fixed null file sent back (Issue #142)
@shliama
Copy link
Contributor

shliama commented Sep 8, 2016

@PGMacDesign Oh yeah, thanks for the PR -> merged it already. Will be available soon in the new update. :octocat:

@shliama shliama closed this as completed Sep 8, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants