首页 > 解决方案 > 如何防止应用在 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)

这些错误从未出现在测试中。

标签: androidandroid-glideandroid-bitmap

解决方案


推荐阅读