-1

I want to load the Arwork-Image into my Notification (with MediaStyle).

Using the following code crashes my app with the error:

java.lang.IllegalArgumentException: You must call this method on a background thread

public Bitmap getAlbumArtwork(long albumID, int Height, int Width) {
    try {
        return Glide.with(mContext).load(getAlbumArtworkUri(albumID)).asBitmap().error(R.drawable.standardartwork).into(Width, Height).get();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    }

    return null;
}

I'm also willing to change the library, e.g. to UniversalImageLoader, if necessary!

Additionally

I use this method to load the image for a notification... This notification is created in a service, so the ui will not be interrupted! As i have no ID of the notifications ImageView, I don't know how to set it in another way! Furthermore I need the Bitmap to add it to the MediaMetaData for the MediaSession.

Cœur
  • 37,241
  • 25
  • 195
  • 267
the_dani
  • 2,466
  • 2
  • 20
  • 46

3 Answers3

2

You must call this method on a background thread

val bitmap = Glide.with(CONTEXT)
            .asBitmap()
            .load("URL")
            .submit().get()
murgupluoglu
  • 6,524
  • 4
  • 33
  • 43
0

How can I load a Bitmap syncronously with Glide?

If you did intend to retrieve the network image from the Main thread (aka UI thread) , that would be a bad approach. Operations that take considerable time should be executed from a separate thread, or in the worse case it can even lead to an ANR (Application Not Responding) state.

If you need the Bitmap from Glide, you can retrieve it perfect in an asynchronous manner, this is how you do it:

Glide.with(mContext)
            .load("http://example.com/imageurl")
            .asBitmap()
            .into(new SimpleTarget<Bitmap>() {
                @Override
                public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                    // you can do something with loaded bitmap here

                    // .....

                    holder.mImageView.setImageBitmap(resource);
                }
            });
OBX
  • 6,044
  • 7
  • 33
  • 77
0

I have same problem, my target server is a single thread embedded-device, so we need send request one by one. after some research I find this work for me.

public class GlideConfModule implements GlideModule {
    @Override
    public void applyOptions(Context context, GlideBuilder builder) {
        //The number "1" make things happen.
        builder.setResizeService(new FifoPriorityThreadPoolExecutor(1));
    }

    @Override
    public void registerComponents(Context context, Glide glide) {}
}

And done forget put this 'define' code to your Manifest.xml

        <application ...
        <meta-data
        android:name="path.to.your.GlideConfModule"
        android:value="GlideModule" />

BTW, I use Glide 3.x

Vicky
  • 11
  • 3