android - 如何防止应用在 Glide onResourceReady 上崩溃
问题描述
我正在尝试从 URL 加载图像并将其设置为墙纸。我在测试中没有遇到任何崩溃,但 firebase 报告在生产中显示以下内容
Glide.with(this)
.asBitmap()
.load(full)
.dontTransform()
.format(DecodeFormat.PREFER_ARGB_8888)
.apply(new RequestOptions().fitCenter())
.into(new CustomTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
Bitmap dstBmp;
if (prefs.getBoolean(SETTINGS_SCROLLABLE_WALL_KEY, false)) {
int width = resource.getWidth();
width = (width * displayHeight / resource.getHeight());
dstBmp = Bitmap.createScaledBitmap(resource, width, displayHeight, true);
} else {
dstBmp = ThumbnailUtils.extractThumbnail(resource, sharedpref.DisplayWidth(), sharedpref.DisplayHeight());
}
WallpaperManager wallManager = WallpaperManager.getInstance(getApplicationContext());
try {
wallManager.setBitmap(dstBmp);
fabProgressCircle.beginFinalAnimation();
fab.setClickable(true);
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(Fullscreen.this, "Error applying image", Toast.LENGTH_SHORT).show();
}
}
这些是在 Firebase 上显示的错误
Fatal Exception: com.bumptech.glide.load.engine.e: Unexpected exception thrown by non-Glide code
at com.bumptech.glide.load.engine.EngineJob.callCallbackOnResourceReady + 154(EngineJob.java:154)
at com.bumptech.glide.load.engine.EngineJob$CallResourceReady.run + 398(EngineJob.java:398)
at android.os.Handler.handleCallback + 873(Handler.java:873)
at android.os.Handler.dispatchMessage + 99(Handler.java:99)
at android.os.Looper.loop + 193(Looper.java:193)
at android.app.ActivityThread.main + 6724(ActivityThread.java:6724)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run + 495(RuntimeInit.java:495)
at com.android.internal.os.ZygoteInit.main + 858(ZygoteInit.java:858)
我猜这个后面跟着上面的错误说明Glide返回的位图的高度或宽度是0
Caused by java.lang.IllegalArgumentException: width and height must be > 0
at android.graphics.Bitmap.createBitmap + 1033(Bitmap.java:1033)
at android.graphics.Bitmap.createBitmap + 1000(Bitmap.java:1000)
at android.graphics.Bitmap.createBitmap + 950(Bitmap.java:950)
at android.graphics.Bitmap.createBitmap + 871(Bitmap.java:871)
at android.media.ThumbnailUtils.transform + 428(ThumbnailUtils.java:428)
at android.media.ThumbnailUtils.extractThumbnail + 231(ThumbnailUtils.java:231)
at android.media.ThumbnailUtils.extractThumbnail + 206(ThumbnailUtils.java:206)
这些错误从未出现在测试中。
解决方案
推荐阅读
- python-3.x - 如何正确使用双循环?
- html - 用固定的图像比例制作相等的正方形,无需任何滚动
- pandas - 仅当目标数据框中的目标字段为空白时,才从 pandas 数据框中的一列复制值
- web-services - 如何在 HTTPRIO 客户端应用程序中发送/选择客户端证书?
- typeorm - 为 typeorm 库和 vue.js 项目创建一个基本的管理器类
- python - HTML 按钮对象未转到预期链接
- java - LinkedHashMap - 获取插入项的索引
- javascript - eslint 忽略默认包的规则
- oracle - Oracle APEX - 如何不转义 IG 列中的特殊字符
- python - python中的扫雷设置值问题