Libgdx加载Glide下载的损坏PNG抛依赖加载错误,求技术解答
解决libgdx AssetManager加载损坏图片时的报错问题
这个场景确实有点棘手——毕竟你已经接受了偶尔会出现损坏文件的情况,但libgdx的AssetManager偏要在加载时报错打断流程对吧?我给你几个针对性的解决方案,你可以根据自己的项目情况挑选:
方案一:保存Bitmap时提前校验,从源头避免损坏文件
既然你是通过Glide的SimpleTarget获取Bitmap后保存,那完全可以在保存前先验证Bitmap的有效性,把无效资源直接过滤掉:
- 先检查Bitmap是否为
null,或者是否已经被回收(调用isRecycled()) - 再通过
getWidth()、getHeight()判断是不是有效尺寸,毕竟正常图片宽高不可能小于等于0 - 要是验证不通过,直接跳过保存,或者给文件加个标记(比如后缀
_corrupted)方便后续识别
举个代码例子:
new SimpleTarget<Bitmap>(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL) { @Override public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) { // 先做有效性校验 if (resource == null || resource.isRecycled() || resource.getWidth() <= 0 || resource.getHeight() <= 0) { // 无效Bitmap,直接跳过保存 return; } // 校验通过,执行正常保存逻辑 saveBitmapToFile(resource, filePath); } @Override public void onLoadFailed(Exception e, Drawable errorDrawable) { // 下载失败的情况直接忽略或者标记 super.onLoadFailed(e, errorDrawable); } };
方案二:给AssetManager加个“容错加载器”,出错时返回占位图
要是已经存在损坏文件不想提前处理,那可以给AssetManager自定义一个加载器,让它在遇到损坏资源时不抛出错误,而是返回你预设的占位图:
第一步:自定义容错TextureLoader
继承libgdx自带的TextureLoader,重写加载方法捕获异常:
public class FaultTolerantTextureLoader extends TextureLoader { private Texture defaultPlaceholder; public FaultTolerantTextureLoader(FileHandleResolver resolver, Texture placeholder) { super(resolver); this.defaultPlaceholder = placeholder; } @Override public Texture load(AssetManager manager, String fileName, FileHandle file, TextureParameter parameter) { try { // 正常加载逻辑 return super.load(manager, fileName, file, parameter); } catch (Exception e) { // 加载失败时打个日志,返回占位图 Gdx.app.error("TextureLoader", "加载损坏文件失败: " + fileName, e); return defaultPlaceholder; } } }
第二步:把自定义加载器注册到AssetManager
// 先加载你准备好的占位图(比如内置的错误提示图) Texture placeholder = new Texture(Gdx.files.internal("placeholder_error.png")); // 初始化AssetManager并替换默认的Texture加载器 AssetManager assetManager = new AssetManager(); assetManager.setLoader(Texture.class, new FaultTolerantTextureLoader(new InternalFileHandleResolver(), placeholder));
这样以后再遇到损坏文件,AssetManager就不会抛出“Couldn't load dependencies of asset”的错误了,直接用占位图顶替,流程能正常走下去。
方案三:加载前手动校验文件有效性
在调用assetManager.load()之前,先自己检查文件是不是有效的PNG:
- 最简单的是检查文件头:PNG文件的前8个字节固定是
89 50 4E 47 0D 0A 1A 0A(十六进制) - 还可以用libgdx的
Pixmap尝试加载一下,能成功加载就是有效文件
校验代码示例:
public boolean isValidPngFile(FileHandle file) { // 先检查文件是否存在、大小是否符合基本要求 if (!file.exists() || file.length() < 8) { return false; } // 校验PNG文件头 byte[] header = new byte[8]; file.read(header); byte[] expectedHeader = {(byte)0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A}; if (!Arrays.equals(header, expectedHeader)) { return false; } // 进一步用Pixmap验证 try { Pixmap pixmap = new Pixmap(file); pixmap.dispose(); return true; } catch (Exception e) { return false; } }
然后加载前判断:
FileHandle targetFile = Gdx.files.internal("your_target_image.png"); if (isValidPngFile(targetFile)) { assetManager.load("your_target_image.png", Texture.class); } else { // 加载占位图或者做其他处理 assetManager.load("placeholder_error.png", Texture.class); }
额外小提示
- 如果你不需要保留损坏文件,也可以在检测到无效后直接删除,省得后续加载添乱
- 另外提一句,Glide v3确实有点年头了,如果项目允许的话,升级到Glide v4会有更完善的错误处理机制,但如果依赖限制不能升级,上面的方案也完全够用
内容的提问来源于stack exchange,提问作者Serge Veiler
相关产品推荐
相关产品推荐

