https://kotlinlang.org logo
Title
e

Elio Maroun

05/23/2023, 12:23 PM
Hello, I want to load an image in jetpack compose, but the url redirects me to the image that's how the backend wants it, how can i achieve it using Coil library. So the url in the AsyncImage will redirect me to the image.
c

Colton Idle

05/23/2023, 12:40 PM
Do you have an example of the URL?
e

Elio Maroun

05/23/2023, 12:42 PM
my url will redirect to a temporary url using amazon s3
c

Colton Idle

05/23/2023, 12:43 PM
I think that should work fine. Coil just uses okhttp under the hood to follow redirects and fetch the image IIRC
e

Elio Maroun

05/23/2023, 12:44 PM
the thing is if I print the image, in the logs I click on the url and it redirects me
but coil is not
c

Colton Idle

05/23/2023, 12:44 PM
did you try enabling logging in coil to see if it gives you any helpful error messages?
e

Elio Maroun

05/23/2023, 12:45 PM
no i did not
@Composable
fun PatientImagesView(model: String, onClick: () -> Unit) {
    AsyncImage(
        model = ImageRequest.Builder(LocalContext.current)
            .data(model)
            .build(),
        contentDescription = "attached image",
        contentScale = ContentScale.Crop,
        modifier = Modifier
            .size(60.dp)
            .clickable { onClick() })
}
j

Joel Denke

05/23/2023, 1:00 PM
Does S3 using some kind of auth headers maybe? In that case maybe need to add headers into ImageLoader or ImageRequest.
e

Elio Maroun

05/23/2023, 1:07 PM
val imageLoader = LocalContext.current.imageLoader.newBuilder()
        .logger(DebugLogger())
        .build()
    AsyncImage(
        model = ImageRequest.Builder(LocalContext.current)
            .data(model)
            .build(),
        imageLoader = imageLoader,
        contentDescription = "attached image",
        contentScale = ContentScale.Crop,
        modifier = Modifier
            .size(60.dp)
            .clickable { onClick() })
}
i did not face any issue
but sill not loading
j

Joel Denke

05/23/2023, 1:39 PM
Could it be related to this? https://github.com/coil-kt/coil/issues/1699
y

yschimke

05/23/2023, 6:29 PM
If you really need to, say the response is literally a string with a new url to fetch, then you can always do this with a custom OkHttp Interceptor. I'd suggest making a request in a test program just using OkHttpClient and see you are getting the right result (bytes, content-length, content-type etc)
c

Colton Idle

05/23/2023, 9:19 PM
what is the output of the debug logger?
does it say success? or give you any failures