I am developing a simple Widget for my Android app based on the following Google StackWidget example .
I am using the Glide image library and trying to populate an ImageView method from getViewAt
a class StackWidgetService
that extends RemoteViewsService
. My code is similar to:
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 is the best way to load an image from a URL to populate a RemoteView of an Android Widget?
Solution
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();
}