Search code examples
javaandroidexceptionandroid-glide

Glide crashes app on loading an animated jpg


So, I'm loading photos from imgur using Glide with the code below.

try {
    ViewTarget viewTarget = new ViewTarget<SubsamplingScaleImageView, BitmapDrawable>(image_view) {
        @Override
        public void onResourceReady(BitmapDrawable bitmap, Transition<? super BitmapDrawable> transition) {
            this.view.setImage(ImageSource.bitmap(bitmap.getBitmap()));
        }
    };

    GlideApp.with(this).load(post_to_show.getUrl()).into(viewTarget);
} catch (ClassCastException e){}

where SubsamplingScaleImageView is - well - a Custom ImageView extending View. But it doesn't matter here.

It's working fine, until I try loading something like this

It has a .jpg extension, but it's a gif.

I get this error:

E/AndroidRuntime: FATAL EXCEPTION: main Process: me.ekhaled667.ridit, PID: 6202 java.lang.ClassCastException: com.bumptech.glide.load.resource.gif.GifDrawable cannot be cast to android.graphics.drawable.BitmapDrawable at me.ekhaled667.ridit.PostFragment$1.onResourceReady(PostFragment.java:257) at com.bumptech.glide.request.SingleRequest.onResourceReady(SingleRequest.java:579) at com.bumptech.glide.request.SingleRequest.onResourceReady(SingleRequest.java:549) at com.bumptech.glide.load.engine.EngineJob.handleResultOnMainThread(EngineJob.java:218) at com.bumptech.glide.load.engine.EngineJob$MainThreadCallback.handleMessage(EngineJob.java:324) at android.os.Handler.dispatchMessage(Handler.java:98) at android.os.Looper.loop(Looper.java:163) at android.app.ActivityThread.main(ActivityThread.java:6348) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:880) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:770)

First: why wasn't it caught as an exception?

Second: How can I possibly go around this, or put another way, know that this is a gif not a jpg and load it properly?


Solution

  • So I figured a way for this to work thanks to @Kaushal Gosaliya, but GIFs will be displayed as static images only.

    ViewTarget viewTarget = new ViewTarget<SubsamplingScaleImageView, Drawable>(image_view) {
                @Override
                public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                    Bitmap mutableBitmap = Bitmap.createBitmap(resource.getIntrinsicWidth(), resource.getIntrinsicHeight(), Bitmap.Config.RGB_565);
                    Canvas canvas = new Canvas(mutableBitmap);
                    resource.setBounds(0, 0, resource.getIntrinsicWidth(), resource.getIntrinsicHeight());
                    resource.draw(canvas);
    
                    this.view.setImage(ImageSource.bitmap(mutableBitmap));
                }
            };
    

    This is the ViewTarget you want to use, where you change BitmapDrawable to Drawable to encompass all drawables. Then you convert the drawable to a bitmap.