You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

同一张图片Glide加载正常,Coil报错的原因及解决求助

Coil加载网络图片失败,Glide可正常加载的问题解决

尝试使用Coil加载网络图片时失败并抛出异常,同一图片使用Glide可正常加载,希望排查并解决Coil的问题,偏好使用Coil完成图片加载任务。

报错日志

2024-12-29 08:29:07.156  7304-7304  RealImageLoader         com.example.messagesendingapp        E  🚨 Failed - https://cdn.pixabay.com/photo/2024/11/24/06/33/fireworks-9220290_960_720.jpg
2024-12-29 08:29:07.157  7304-7304  RealImageLoader         com.example.messagesendingapp        E  java.lang.IllegalStateException: Unable to create a fetcher that supports: https://cdn.pixabay.com/photo/2024/11/24/06/33/fireworks-9220290_960_720.jpg (Ask Gemini)
                                                                                                        at coil3.intercept.EngineInterceptor.fetch(EngineInterceptor.kt:153)
                                                                                                        at coil3.intercept.EngineInterceptor.execute(EngineInterceptor.kt:115)
                                                                                                        at coil3.intercept.EngineInterceptor.access$execute(EngineInterceptor.kt:32)
                                                                                                        at coil3.intercept.EngineInterceptor$intercept$2.invokeSuspend(EngineInterceptor.kt:66)
                                                                                                        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
                                                                                                        at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:101)
                                                                                                        at kotlinx.coroutines.internal.LimitedDispatcher$Worker.run(LimitedDispatcher.kt:113)
                                                                                                        at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:89)
                                                                                                        at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:589)
                                                                                                        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:823)
                                                                                                        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:720)
                                                                                                        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:707)

Coil代码实现

val imageLoader = ImageLoader.Builder(LocalContext.current)
                    .logger(DebugLogger())
                    .diskCachePolicy(CachePolicy.ENABLED)
                    .build()

AsyncImage(
    model = ImageRequest.Builder(LocalContext.current)
        .data("https://cdn.pixabay.com/photo/2024/11/24/06/33/fireworks-9220290_960_720.jpg")
        .build(),
    imageLoader = imageLoader,
    placeholder = painterResource(R.drawable.audio_icon),
    error = painterResource(id = R.drawable.info_image),
    contentDescription = "Profile Fhoto",
    contentScale = ContentScale.Crop,
    modifier = Modifier
        // Set image size to 40 dp
        .size(40.dp)
        .width(48.dp)
        .height(48.dp)
        // Clip image to be shaped as a circle
        .clip(CircleShape)
        .border(1.5.dp, MaterialTheme.colorScheme.primary, CircleShape)
)

Glide代码实现

GlideImage(
    imageModel = { "https://cdn.pixabay.com/photo/2024/11/24/06/33/fireworks-9220290_960_720.jpg" },
    imageOptions = ImageOptions(contentDescription = "Fireworks"),
    loading = {
        CircularProgressIndicator(color = Color.Gray)
    },
    failure = {
        // Show a placeholder or error content
        Box(modifier = Modifier.size(200.dp)) {
            androidx.compose.material3.Text(text = "Image Failed")
        }
    }
)

使用的依赖版本

coilCompose = "3.0.4"
glide = "4.15.1"

解决方案

报错Unable to create a fetcher that supports的核心原因是Coil缺少处理网络URL的能力——Coil 3.x版本将网络加载模块拆分到独立依赖库中,默认未包含在coil-compose依赖里。

步骤1:添加coil-network依赖

在项目的build.gradle或build.gradle.kts中添加与coil-compose版本一致的coil-network依赖:

// build.gradle.kts
implementation("io.coil-kt:coil-network:3.0.4")
// build.gradle
implementation 'io.coil-kt:coil-network:3.0.4'

步骤2:配置ImageLoader启用网络加载

修改ImageLoader初始化代码,添加NetworkInterceptor以启用网络图片加载功能:

val imageLoader = ImageLoader.Builder(LocalContext.current)
    .logger(DebugLogger())
    .diskCachePolicy(CachePolicy.ENABLED)
    .addInterceptor(coil3.network.NetworkInterceptor.Factory()) // 新增该行配置
    .build()

完成以上两步后重新构建项目,Coil即可正常加载网络图片。


内容的提问来源于stack exchange,提问作者Prakhar_Pathak

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 06:34:54