CanHub / Android-Image-Cropper

Image Cropping Library for Android, optimised for Camera / Gallery.
https://canhub.github.io/
Apache License 2.0
1.17k stars 240 forks source link

Android 13 (SDK 33): CropImageView white on some Samsung devices #599

Open manuel-tud opened 8 months ago

manuel-tud commented 8 months ago

Hi, I've an app with 100k+ installs out there. Recently I've updated the target SDK version to 33 without changing anything on the image cropping code. Since that update I receive lots of complaints from users with various (not all) Samsung devices running Android 13 saying that the CIV doesn't load the chosen image and just stays white. I load the image using the setImageUriAsync() method. Unfortunately I was not able to reproduce that bug on my test devices (which are not made by Samsung). I was also not able to reproduce it using Samsung's Test Lab devices. But by the amount of bug report messages (including screenshots showing the behavior) I get, I can say for sure, that there is a bug.

(This could by the same bug mentioned here: https://github.com/CanHub/Android-Image-Cropper/issues/593)

vanniktech commented 8 months ago

Can you reproduce and try to submit a PR which fixes this?

anmol14dev commented 7 months ago

Facing same issue on my samsung device. I can see the image with crop square but white screen all around.

Devenom1 commented 6 months ago

Same issue here. But not just Samsung Phones. Motorola phones too. I don't have direct access to the devices facing the issue.

HamzaAkram-shyk commented 6 months ago

Facing the same issue on my Vivo device. I can see the image with a crop square but a white screen all around. there is no option to go back and get the cropped image's Uri.

kevschweitzer commented 6 months ago

Same issue here. I Could handle it by implementing a CustomCropImageActivity as stated in documentation and in sample app.

Custom activity code

import android.net.Uri
import android.os.Bundle
import com.canhub.cropper.CropImageActivity
import dac.corp.dactive.databinding.ActivityCustomCropImageBinding

class CustomCropImageActivity : CropImageActivity() {

    private lateinit var binding: ActivityCustomCropImageBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        binding = ActivityCustomCropImageBinding.inflate(layoutInflater)
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
        setListener()
        setCropImageView(binding.cropImageView)
    }

    private fun setListener() {
        binding.button.setOnClickListener { cropImage() }
    }

    override fun onPickImageResult(resultUri: Uri?) {
        super.onPickImageResult(resultUri)

        if (resultUri != null) {
            binding.cropImageView.setImageUriAsync(resultUri)
        }
    }
}

Custom Activity Layout

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".utilities.CustomCropImage.CustomCropImageActivity">

    <com.canhub.cropper.CropImageView
        android:id="@+id/cropImageView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@id/button"/>

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Accept"
        app:layout_constraintTop_toBottomOf="@id/cropImageView"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

And this is the code to start the activity.

  1. You register the activity for result with a new Contract I created to change the Activity that will start (to put my own). I left it below
  2. You launch the activity in some button with the options you want (in my case I only wanted gallery and a square aspect ratio).
private val cropImage = registerForActivityResult(CustomCropImageContract()) { result ->
        if (result.isSuccessful) {
            result.uriContent?.let {
                val bitmap = mapUriToBitmap(it)
                viewModel.setNewPhoto(bitmap)
            }
        }
    }

//In some button you launch the activity
cropImage.launch(
                CropImageContractOptions(null, CropImageOptions(
                    imageSourceIncludeGallery = true,
                    imageSourceIncludeCamera= false,
                    fixAspectRatio = true
                ))
            )

The custom contract here

class CustomCropImageContract : ActivityResultContract<CropImageContractOptions, CropImageView.CropResult>() {
    override fun createIntent(context: Context, input: CropImageContractOptions) = Intent(context, CustomCropImageActivity::class.java).apply {
        putExtra(
            CropImage.CROP_IMAGE_EXTRA_BUNDLE,
            Bundle(2).apply {
                putParcelable(CropImage.CROP_IMAGE_EXTRA_SOURCE, input.uri)
                putParcelable(CropImage.CROP_IMAGE_EXTRA_OPTIONS, input.cropImageOptions)
            },
        )
    }

    override fun parseResult(
        resultCode: Int,
        intent: Intent?,
    ): CropImageView.CropResult {
        val result = intent?.parcelable<CropImage.ActivityResult>(CropImage.CROP_IMAGE_EXTRA_RESULT)

        return if (result == null || resultCode == Activity.RESULT_CANCELED) {
            CropImage.CancelledResult
        } else {
            result
        }
    }
}
Alex-Dobrynin commented 6 months ago

@vanniktech Same here. Will this be fixed?

SourabhSuman007 commented 1 month ago

@vanniktech Is this issue fixed in the latest release?

vanniktech commented 1 month ago

Has something changed since my last comment? https://github.com/CanHub/Android-Image-Cropper/issues/599#issuecomment-1799260477

omiwrench commented 1 week ago

Same issue here, using a Pixel 6 emulator with API 34.