Hot questions for Using Glide in remoteview

Question:

I'm developing a simple widget to my android app based on google "StackWidget" sample: https://android.googlesource.com/platform/development/+/master/samples/StackWidget/src/com/example/android/stackwidget/StackWidgetService.java

I'm using glide image library and I'm trying to populate an ImageView on getViewAt method of StackWidgetService class that extends RemoteViewsService. I'm doing something like this but it's not working:

    Handler uiHandler = new Handler(Looper.getMainLooper());
    uiHandler.post(() ->
            Glide.with(context)
            .asBitmap()
            .load(widgetItems.get(position).image_url)
            .into(new SimpleTarget<Bitmap>(512, 512) {
                @Override
                public void onResourceReady(Bitmap bitmap, Transition transition) {
                    rv.setImageViewBitmap(R.id.widget_item_image, bitmap);
                }
            }));

What's the correct and best way of loading images from an URL to populate a RemoteView from an Android Widget?


Answer:

Glide has a construct for doing this called: AppWidgetTarget:

In Kotlin:

    val awt: AppWidgetTarget = object : AppWidgetTarget(context.applicationContext, R.id.img, remoteViews, *appWidgetIds) {
        override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
            super.onResourceReady(resource, transition)
        }
    }

    var options = RequestOptions().
            override(300, 300).placeholder(R.drawable.placeholder_img).error(R.drawable.error_img)

    Glide.with(context.applicationContext).asBitmap().load(imageUrl).apply(options).into(awt)

In Java:

   AppWidgetTarget awt = new AppWidgetTarget(context, R.id.img_dog, remoteViews, appWidgetIds) {
        @Override
        public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
            super.onResourceReady(resource, transition);
        }
    };

    RequestOptions options = new RequestOptions().
            override(300, 300).placeholder(R.drawable.placeholder_img).error(R.drawable.error_img)


    Glide.with(context.getApplicationContext())
            .asBitmap()
            .load(imageUrl)
            .apply(options)
            .into(awt);

Question:

I am trying to load an image to a push notification using Glide but it says this:

FATAL EXCEPTION: Thread-9730
Process: com.monkingme.monkingmeapp, PID: 24226
java.lang.IllegalArgumentException: You must call this method on the main thread at com.bumptech.glide.util.Util.assertMainThread(Util.java:135)                                                                                

And the code used:

NotificationTarget notificationTarget = new NotificationTarget(
                context,
                rv,
                R.id.remoteview_notification_icon,
                notification,
                NOTIFICATION_ID);

Glide.with(context.getApplicationContext())
     .load(item.getString("cover_img"))
     .asBitmap()
     .placeholder(placeholder)
     .error(placeholder)
     .into(notificationTarget);

I am using MessageHandler from Aerogear --> https://aerogear.org/docs/guides/aerogear-android/push/

The thing is that in a push notification the app is not running, so there isn't a main thread. Any suggestion?


Answer:

Try that way:

    new Handler(Looper.getMainLooper()).post(new Runnable() {
        @Override 
            public void run() {
                 Glide.with(context.getApplicationContext())
                    .load(item.getString("cover_img"))
                    .asBitmap()
                    .placeholder(placeholder)
                    .error(placeholder)
                    .into(notificationTarget);
        }
    });