Reputation: 81
I am trying to fetch image from url using Coil Image Loader but it is not working.
I tried the code on Coil's own site, but it didn't work.
First I get the drawable with the code block below.
val request = ImageRequest.Builder(context)
.data(url)
.memoryCachePolicy(CachePolicy.ENABLED)
.error(R.drawable.error_image)
.target { drawable ->
callback(drawable)
}
.build()
val disposable = ImageLoader(context)
disposable.enqueue(request)
Then I pass it to "Image" in the code block below. But the picture does not come, an empty pink color comes out. I checked the url, the error is not from him. Drawable does not come empty, it takes data. Even the data is the same as the image dimensions in the url. But I couldn't solve the problem.
Image(
painter = rememberAsyncImagePainter(drawable),
contentDescription = null,
contentScale = ContentScale.Fit,
modifier = modifier
)
Upvotes: 0
Views: 1372
Reputation: 7278
You don't specify what does callback(drawable)
do and where you actually call enqueue(request)
- this shouldn't be done inside composable function (without LaunchedEffect or something). But the main point is that you probably don't have to do it so complicated.
You can just create the request (without target
):
val request = ImageRequest.Builder(context)
.data(url)
.memoryCachePolicy(CachePolicy.ENABLED)
.error(R.drawable.error_image)
.build()
And set it to your image:
Image(painter = rememberAsyncImagePainter(request))
// or even:
AsyncImage(model = request)
Upvotes: 0