2 years ago

#33677

test-img

tyczj

Using Coil ImageRequest target causes image not to load

In the Coil image library I am trying to get when the image has been successfully loaded and then use Palette to get the colors from the bitmap but I am having a problem where if I use the ImageRequests's target the image does not show in the imageview but the onSuccess completes correctly because I am able to extract the color from the given bitmap

Here is what I have

_contentImage.load(File(image))
    {
        crossfade(true)
        allowHardware(false)
        listener(onError = { _, error ->
            Log.d(SUBTAG, "Error -> ${error.message}")
        },
        onSuccess = {_, _ ->
            Log.d(SUBTAG, "Content image loaded")
            contentImageLoaded = true
            checkContentLoaded()
        })
        target(
            onSuccess = { result ->
                Palette.from(result.toBitmap()).generate {
                    if (it != null) {
                        val paletteColor = it.getDominantColor(color)
                        _constraintLayout.setBackgroundColor(paletteColor)
                    }
                }
            }
        )
    }

If I comment out the whole target section of the builder the image shows fine but obviously I don't get the palette stuff

Do I have to manually set the drawable to the image if I use the onSuccess like this

    target(
        onSuccess = { result ->
            Palette.from(result.toBitmap()).generate {
                if (it != null) {
                    val paletteColor = it.getDominantColor(color)
                    _constraintLayout.setBackgroundColor(paletteColor)
                }
            }
            _contentImage.setImageDrawable(result) // Set it here?
        }
    )

If I do that the image shows but it seems odd to have to manually set it and I feel like it should be set by itself

Not sure what the problem is here

android

coil

0 Answers

Your Answer

Accepted video resources