Load ImageView from URL into RemoteView of a Home Screen Widget

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);
  • Documentation: https://futurestud.io/tutorials/glide-loading-images-into-notifications-and-appwidgets
  • Tested on the current version of Glide 4.7.1

Just need to do it synchronously. This seems to work fine:

    try {
        Bitmap bitmap = Glide.with(context)
                .asBitmap()
                .load(widgetItems.get(position).image_url)
                .submit(512, 512)
                .get();

        rv.setImageViewBitmap(R.id.widget_item_image, bitmap);
    } catch (Exception e) {
        e.printStackTrace();
    }