Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load image with Glide for AppWidget

I'm working on my first app widget. Everthing is fine except one thing. I can't find out, how can I load image into ImageView at RemoteViewsFactroy's getViewAt() method with Glide. I allways get error, because I've to run into(AppWidgetTarget) at MainThread, but I can't call new Handler(Looper.getMainLooper()) from RemoteViewsFactory.

like image 588
user3057944 Avatar asked Oct 30 '22 17:10

user3057944


1 Answers

Glide has support for this by providing the AppWidgetTarget class.

While updating your widget in onUpdate(), you create a new AppWidgetTarget, pass it the RemoteViews object, the id of your image view, and the appwidgetId and then invoke glide in nearly the same way as you would for an imageview in your app.

    appWidgetTarget = new AppWidgetTarget( context, rv, R.id.custom_view_image, appWidgetIds );

    Glide
            .with( context.getApplicationContext() ) // safer!
            .load( GlideExampleActivity.eatFoodyImages[3] )
            .asBitmap()
            .into( appWidgetTarget );

Example code above taken from this wonderful set of glide tutorials. The tutorials cover many practical aspects of using glide.

Similarly, a NotificationTarget target class is also provided by glide - it solves the same problem, but for notifications instead of appWidgets.

like image 146
rothloup Avatar answered Nov 15 '22 06:11

rothloup