question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Get the Drawable from a success request on Compose

See original GitHub issue

Is your feature request related to a problem? Please describe.

There’s a discussion on #851 about getting the drawable from the rememberImagePainter in order to use on other methods (like getting the color palette) without making another request externally.

The best way of doing that would be using the target from the ImageRequest. But, as seen on the discussion, it is set internally so it’s not possible to use that on the image request builder inside the rememberImagePainter

    val painter = rememberImagePainter(
        imageURL,
        builder = {
            target( // target must be null
                onSuccess = {
                    doStuffWithDrawable(it)
                }
            )
        },
    )

Describe the solution you’d like

A solution i see to that would be a callback like the ImageRequest’s target on the compose function, like so:

    val painter = rememberImagePainter(
        imageURL,
        callbacks = {
            onSuccess { drawable ->
                doStuffWithDrawable(drawable)
            }
        }
    )

The internal target would pipe the the onStart, onError and onSuccess from the internal target to the callbacks on the remember function.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:3
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
colinrtwhitecommented, Sep 15, 2021

@univeous Ah! Sorry I totally got this wrong and it’s not possible to access the bitmap in BitmapPainter. Given the other solutions are hacky, I’m going to push 1.3.3 today which expose the Drawable in ImagePainter.state. That way you’ll be able to get the drawable using:

val painter = rememberImagePainter("https://www.example.com/image.jpg")
val drawable = (painter.state.result as? SuccessResult)?.drawable
Image(painter = painter, contentDescription = null)

EDIT: Sorry for the delay. I’ve held of rolling out the update temporarily to verify that adding this attribute doesn’t increase recompositions.

1reaction
colinrtwhitecommented, Aug 17, 2021

This should be easier in 2.0 since listener has the image result. The best way to grab the bitmap currently is to use ((rememberImagePainter(data).state.painter) as? BitmapPainter).bitmap

Read more comments on GitHub >

github_iconTop Results From Across the Web

In Compose how to access drawable once Coil loads image ...
Hint: You can use this Composable to Load and Render your Image from Url and get your Image Palette by the returned Drawable....
Read more >
Loading images for Jetpack Compose using Glide, Coil, and ...
Simply fetching images from an Url (network) and drawing on the Image composable would require quite a completely different process from before. At...
Read more >
Loading Image in Android Jetpack Compose Made Easy
How to load image bitmap, vector, or from URL in Jetpack Compose ... It can be easily setup to handle Loading, Error and...
Read more >
Resources in Compose - Android Developers
Resources in Compose · On this page · Strings. String plurals (experimental) · Dimensions · Colors · Vector assets and image resources ·...
Read more >
Exploring Images in Jetpack Compose - Instacart
Jetpack Compose was announced at Google IO 2019 and is going to change ... Coil.loader().get(request) image.value = AndroidImage(drawable.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found