1

I am trying to run below code to get cover image, if no cover is found method returns default drawable (R.drawable.no_cover).

private Bitmap getCoverImage(final Song song) {
    Bitmap bitmap;
    final Uri uri = ContentUris.withAppendedId(Utilities.sArtworkUri, song.getAlbumId());
        try {
            bitmap = Glide.with(service)
                .load(uri)
                .asBitmap()
                .into(256, 256)
                .get();
        } catch (InterruptedException | ExecutionException e) {
            bitmap = BitmapFactory.decodeResource(service.getResources(), R.drawable.no_cover);
        }
    return bitmap;
}

However I get error (log below). Should I create new thread for this??

07-17 08:25:30.508 21095-21095/com.example.app E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.app, PID: 21098
    java.lang.IllegalArgumentException: YOu must call this method on a background thread
        at com.bumptech.glide.util.Util.assertBackgroundThread(Util.java:144)
        at com.bumptech.glide.request.RequestFutureTarget.doGet(RequestFutureTarget.java:169)
        at com.bumptech.glide.request.RequestFutureTarget.get(RequestFutureTarget.java:100)
        at com.example.app.notification.PlayerNotification$1.run(PlayerNotification.java:151)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6682)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
Zoe
  • 27,060
  • 21
  • 118
  • 148
user221256
  • 415
  • 5
  • 14

2 Answers2

0

Because you need this Bitmap for showing a notification, that means, that you have to first fetch the bitmap and later take care of actually displaying the notification.

This is an AsyncTask implementation, which may help you:

public class TestActivity extends Activity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        new MyAsyncTask(getApplicationContext(), "some url").execute();
    }

    private static class MyAsyncTask extends AsyncTask<Void, Void, Bitmap> {

        private Context context;
        private String url;

        MyAsyncTask(Context context, String url) {
            this.context = context;
            this.url = url;
        }

        @Override
        protected Bitmap doInBackground(Void... params) {
            try {
                return Glide.with(context)
                        .load(url)
                        .asBitmap()
                        .into(256, 256)
                        .get();
            } catch (InterruptedException | ExecutionException e) {
                e.printStackTrace();
                return null;
            }
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            super.onPostExecute(bitmap);

            if (null != bitmap) {
                // show notification using bitmap
            } else {
                // couldn't fetch the bitmap
            }
        }
    }
}
azizbekian
  • 60,783
  • 13
  • 169
  • 249
0

Most of the API's and methods of Glide are now deprecated. Below is working with Glide 4.9 and upto Android 10.

Since image is loaded from internet, it should always be in a async call or a background thread. You can use a async task or image loading libs like Glide.

To load image notification from a url, you can use the style "NotificationCompat.BigPictureStyle()". This requires a bitmap (which has to be extracted from image url)

Display the image notification once, the bitmap is ready.

You can override default error Glide methods to show R.drawable.no_cover

// Load bitmap from image url on background thread and display image notification
    private void getBitmapAsyncAndDoWork(String imageUrl) {

        final Bitmap[] bitmap = {null};

        Glide.with(getApplicationContext())
                .asBitmap()
                .load(imageUrl)
                .into(new CustomTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {

                        bitmap[0] = resource;
                        // TODO Do some work: pass this bitmap
                        displayImageNotification(bitmap[0]);
                    }

                    @Override
                    public void onLoadCleared(@Nullable Drawable placeholder) {
                    }
                });
    }
Mayuri Khinvasara
  • 1,437
  • 1
  • 16
  • 12