Drjacky / ImagePicker

📸Image Picker for Android, Pick images from Gallery or Capture a new image with Camera🖼
https://github.com/Drjacky/ImagePicker
Apache License 2.0
232 stars 57 forks source link

Quick question about Camera and Gallery mode #65

Closed voghDev closed 2 years ago

voghDev commented 2 years ago

Hello!

first of all, thanks for this awesome library, it's doing a great task. I just want to ask a question about the documentation in the README file. The example code provided is the following :point_down:

        pickImageLauncher.launch(
            ImagePicker.with(requireActivity())
                .createIntent()
        )

it states: If you want both Camera and Gallery

But in my case, the code directly uses the camera instead of allowing me to choose between camera and Gallery. It is working well though. :question: :grey_question: Is this the intended behaviour? :question: :grey_question:

Tested devices were Xiaomi Mi A3 (Android 11), Xiaomi Mi A2 Lite (Android 10) and Nexus 5X emulator (Api 26).

Additionally, I see that .compress() was removed. Is there a replacement for it? I'm using maxResultSize but am not sure if it's the best option

Thank you so much!

voghDev commented 2 years ago

A few more information and context in case it helps.

This code worked well allowing us to select a picture from Gallery in the mentioned devices:

    pickImageLauncher.launch(
            ImagePicker.with(requireActivity())
                .maxResultSize(
                    1024,
                    1024,
                    false
                )
                .cropSquare()
                .galleryOnly()
                .galleryMimeTypes(mimeTypes = someMimeTypes)
                .createIntent()
        )

And this other code also worked well taking a picture with Camera:

    pickImageLauncher.launch(
            ImagePicker.with(requireActivity())
                .maxResultSize(
                    1024,
                    1024,
                    false
                )
                .cropSquare()
                .cameraOnly()
                .galleryMimeTypes(mimeTypes = someMimeTypes)
                .createIntent()
        )

where pickImageLauncher is:

private val pickImageLauncher =
        registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
            if (result.resultCode == Activity.RESULT_OK) {
               // ...
           }
        }

I can provide a pull request updating the README file to make the documentation consistent

Drjacky commented 2 years ago

Where do you see this: pickImageLauncher.launch( ?

What I see in the readme.md is:

If you want both Camera and Gallery:

Kotlin

    ImagePicker.with(this)
       //...
       .createIntentFromDialog { launcher.launch(it) }

Additionally, I see that .compress(). Is there a replacement for it? I'm using maxResultSize but am not sure if it's the best option

compress wasn't work properly so, maxResultSize is enough, I'd say.

voghDev commented 2 years ago

:heavy_check_mark: I finally solved my issue. I'll tell you how. Something really curious was happening :scream: :point_down:

I tried the code on the first comment because the sample code wasn't working for me. When I launched it, nothing was working. A dialog to choose between Camera and Gallery was supposed to appear, and it wasn't appearing :point_down:

ImagePicker.with(requireActivity())
            .maxResultSize(
                1024,
                1024,
                false
            )
            .cropSquare()
            .galleryMimeTypes(mimeTypes = someMimeTypes)
            .createIntentFromDialog { pickImageLauncher.launch(it) }

After introspecting in the library code and setting some breakpoints, I found a curious value for imageProvider in ImagePicker::createIntentFromDialog function:

provider

the imageProvider variable had FRONT_CAMERA as value instead of BOTH, which is the initial value and the expected one I need.

I can't guess why this happened :sweat_smile: I don't see FRONT_CAMERA assigned to imageProvider anywhere, but in the Intent extras. Anyway, all I needed to do was to force imageProvider to have BOTH as value. Code is as follows:

ImagePicker.with(requireActivity())
            .maxResultSize(
                1024,
                1024,
                false
            )
            .provider(ImageProvider.BOTH) // This line is the key
            .cropSquare()
            .galleryMimeTypes(mimeTypes = someMimeTypes)
            .createIntentFromDialog { pickImageLauncher.launch(it) }

Now the dialog is showing and everything works again :slightly_smiling_face: Thank you very much

Drjacky commented 2 years ago

It has been fixed on 2.1.16