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)