1

I am trying to set gif wallpaper but the app crashes. This works fine with simple images(png, jpg)

Error message:

java.lang.ClassCastException: com.bumptech.glide.load.resource.gif.GifDrawable cannot be cast to android.graphics.drawable.BitmapDrawable

Glide.with(this).load(getIntent().getStringExtra("images")).into(imageView);

Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
        WallpaperManager manager =  WallpaperManager.getInstance(getApplicationContext());
        try {
            manager.setBitmap(bitmap);
            Toasty.success(getApplicationContext(), "Set Wallpaper Successfully", Toast.LENGTH_SHORT, true).show();

        }
        catch (IOException e) {
            Toasty.warning(this, "Wallpaper not load yet!", Toast.LENGTH_SHORT, true).show();
        }

I want to set gif wallpaper(Live wallpaper)

Bad Boy
  • 381
  • 2
  • 14
  • Tried removing the cast `(BitmapDrawable)`? Because a GifDrawable can't be casted to a BitmapDrawable – MatPag Mar 23 '20 at 08:15

1 Answers1

2

GifDrawable is a child of Drawable which is returned by getDrawable(), previously loaded using,

Glide.with(this).load(getIntent().getStringExtra("images")).into(imageView);

so GifDrawable and BitmapDrawable are siblings so cannot be casted to each other.

Alternatively, you can use getFirstFrame() to get the Bitmap as

 Bitmap bitmap = ((GifDrawable)imageView.getDrawable()).getFirstFrame();
Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
  • @MarsadMaqsood missed the casting change, use `((GifDrawable)imageView.getDrawable()).getFirstFrame()` – Pavneet_Singh Mar 23 '20 at 08:24
  • ```java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference ``` – Bad Boy Mar 23 '20 at 08:29
  • @MarsadMaqsood first, bitmaps are not gif so you need to write own wallpaper service to draw gis for wallpaper. seems like the image is not loaded yet so use [RequestListener](https://stackoverflow.com/questions/26054420/set-visibility-of-progress-bar-gone-on-completion-of-image-loading-using-glide-l) and move the bit code inside `onResourceReady` – Pavneet_Singh Mar 23 '20 at 08:37
  • @MarsadMaqsood yes, handle the logic of drawing of each frame inside draw but glide gives you access to only first frame, not all frames so might want to use any another lib maybe Fresco can help or you can write your own `ResourceDecoder` for glide to process and access frames – Pavneet_Singh Mar 23 '20 at 08:54