Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Coil don't load image in emulator with Jetpack Compose

I need to show an image in my app by url using Coil, but this image don't load. I follow the official documentation https://coil-kt.github.io/coil/compose/.

profile card

implementation "io.coil-kt:coil-compose:1.3.1"
@Composable
fun ProfilePicture(profilePicture: String, online: Boolean) {
    Card(
        shape = CircleShape,
        border = BorderStroke(
            width = 2.dp,
            color = if (online) MaterialTheme.colors.lightGreen else Color.Red
        ),
        modifier = Modifier.padding(16.dp),
        elevation = 4.dp
    ) {
        Image(
            painter = rememberImagePainter(
                data = profilePicture,
                builder = {
                    transformations(CircleCropTransformation())
                }
            ),
            modifier = Modifier.size(72.dp),
            contentDescription = "Profile picture"
        )
    }
}

Update

An exemplo to UserModel

UserModel(
    name = "John Doe",
    profilePicture = "https://randomuser.me/api/portraits/men/32.jpg",
    online = true
)
like image 951
Gustavo Faria Avatar asked Jul 30 '21 00:07

Gustavo Faria


People also ask

Can you use Java with jetpack compose?

Can I use it with Java? Jetpack Compose is Kotlin exclusive. It uses features such as coroutines, and the handling of @Composable annotations is done by a Kotlin compiler. There is no way to get access to these from Java.

Is jetpack compose reactive?

Jetpack Compose is a modern toolkit designed to simplify UI development. It combines a reactive programming model with the conciseness and ease of use of the Kotlin programming language. It is fully declarative, meaning you describe your UI by calling a series of functions that transform data into a UI hierarchy.


2 Answers

Coil doesn't load images on the emulator because you need to enable clear text traffic, add this line to the application tag in AndroidManifest.xml.

android:usesCleartextTraffic="true"

Then uninstall the application from your emulator and install it again, it will work.

like image 108
shaarawy Avatar answered Oct 19 '22 23:10

shaarawy


I had the same issue, only occurring on the emulator. Turning off mobile data, while leaving Wi-Fi enabled solved the problem for me.

like image 22
Mieszko Koźma Avatar answered Oct 20 '22 01:10

Mieszko Koźma