您现在的位置是:首页 >学无止境 >Glide加载大图片报错问题,记录一个bug,已解决网站首页学无止境
Glide加载大图片报错问题,记录一个bug,已解决
我加载的图片格式是png,大小7MB左右,无法显示
错误信息:
Load failed for https://xxxxxxx.xxxx.xxxx/2023/05/18/155726_6da4823e813b4237a4863927bc5e0cea.png with size [1920x1008]
class com.bumptech.glide.load.engine.GlideException: Failed to load resource
There were 2 root causes:
com.bumptech.glide.load.resource.bitmap.RecyclableBufferedInputStream$InvalidMarkException(Mark has been invalidated, pos: 1516658 markLimit: 5242880)
com.bumptech.glide.load.resource.bitmap.RecyclableBufferedInputStream$InvalidMarkException(Mark has been invalidated, pos: 2299 markLimit: 5242880)
call GlideException#logRootCauses(String) for more detail
Cause (1 of 1): class com.bumptech.glide.load.engine.GlideException: Failed LoadPath{ContentLengthInputStream->Object->Drawable}, REMOTE
There were 2 root causes:
com.bumptech.glide.load.resource.bitmap.RecyclableBufferedInputStream$InvalidMarkException(Mark has been invalidated, pos: 1516658 markLimit: 5242880)
com.bumptech.glide.load.resource.bitmap.RecyclableBufferedInputStream$InvalidMarkException(Mark has been invalidated, pos: 2299 markLimit: 5242880)
call GlideException#logRootCauses(String) for more detail
Cause (1 of 3): class com.bumptech.glide.load.engine.GlideException: Failed DecodePath{ContentLengthInputStream->GifDrawable->Drawable}
Cause (2 of 3): class com.bumptech.glide.load.engine.GlideException: Failed DecodePath{ContentLengthInputStream->Bitmap->Drawable}
There was 1 root cause:
com.bumptech.glide.load.resource.bitmap.RecyclableBufferedInputStream$InvalidMarkException(Mark has been invalidated, pos: 1516658 markLimit: 5242880)
call GlideException#logRootCauses(String) for more detail
Cause (1 of 1): class com.bumptech.glide.load.resource.bitmap.RecyclableBufferedInputStream$InvalidMarkException: Mark has been invalidated, pos: 1516658 markLimit: 5242880
Cause (3 of 3): class com.bumptech.glide.load.engine.GlideException: Failed DecodePath{ContentLengthInputStream->BitmapDrawable->Drawable}
There was 1 root cause:
com.bumptech.glide.load.resource.bitmap.RecyclableBufferedInputStream$InvalidMarkException(Mark has been invalidated, pos: 2299 markLimit: 5242880)
call GlideException#logRootCauses(String) for more detail
Cause (1 of 1): class com.bumptech.glide.load.resource.bitmap.RecyclableBufferedInputStream$InvalidMarkException: Mark has been invalidated, pos: 2299 markLimit: 5242880
2023-05-19 15:54:43.299 18021-18021 Glide com.example.testdemo
解决办法:重新设置图片的格式format(DecodeFormat.PREFER_RGB_565) 代码如下
fun loadImage(context: Context?, url: String?, imageView: ImageView?): ViewTarget<ImageView?, Drawable?>? {
val options: RequestOptions = RequestOptions().placeholder(R.drawable.ic_launcher_background)
.error(R.drawable.ic_launcher_background).format(DecodeFormat.PREFER_RGB_565).diskCacheStrategy(DiskCacheStrategy.ALL)
return Glide.with(context!!)
.load(url)
.apply(options)
.into(imageView!!)
}
如果加载的图片很模糊可以尝试以下代码
fun loadImage(context: Context?, url: String?, imageView: ImageView?): ViewTarget<ImageView?, Drawable?>? {
val options: RequestOptions = RequestOptions()
.placeholder(R.drawable.ic_launcher_background)
.error(R.drawable.ic_launcher_background)
.format(DecodeFormat.PREFER_RGB_565)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
return Glide.with(context!!)
.load(url)
.apply(options)
.into(imageView!!)
}