Update Android widget (using async task) with an image from the Internet

I have a simple widget for Android that I want to update using an image from the Internet. I can not show static images in widgets. I was told that for this you need to use the async task, and I do not have much experience with them.

Here is my widget:

@Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { super.onUpdate(context, appWidgetManager, appWidgetIds); for (int i = 0; i < appWidgetIds.length; i++){ int appWidgetId = appWidgetIds[i]; RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.activity_main); //Setup a static image, this works fine. views.setImageViewResource(R.id.imageView1, R.drawable.wordpress_icon); new DownloadBitmap().execute("MyTestString"); appWidgetManager.updateAppWidget(appWidgetId, views); } 

Then I have an async task class that does the loading. It looks like this:

 public class DownloadBitmap extends AsyncTask<String, Void, Bitmap> { /** The url from where to download the image. */ private String url = "http://0.tqn.com/d/webclipart/1/0/5/l/4/floral-icon-5.jpg"; @Override protected Bitmap doInBackground(String... params) { try { InputStream in = new java.net.URL(url).openStream(); Bitmap bitmap = BitmapFactory.decodeStream(in); return bitmap; //NOTE: it is not thread-safe to set the ImageView from inside this method. It must be done in onPostExecute() } catch (Exception e) { Log.e("ImageDownload", "Download failed: " + e.getMessage()); } return null; } @Override protected void onPostExecute(Bitmap bitmap) { if (isCancelled()) { bitmap = null; } //Here is where I should set the image to the imageview, but how? } } 

I think my code successfully downloaded the image from the Internet.

What I'm confused is how do I get this image in the “ImageView” of a particular widget from my Async task class. To update the image, you need access to 3 different objects: Context, AppWidgetManager and AppWidgetId .... But how to transfer all these objects inside this expression: ???

 new DownloadBitmap().execute("MyTestString"); 

Thanks!

+4
source share
2 answers

One solution would be to pass RemoteViews as an argument to the DownloadBitmap constructor, and in onPostExecute() , set the image:

In onUpdate ():

 new DownloadBitmap(views).execute("MyTestString"); 

and in DownloadBitmap:

 //.... public class DownloadBitmap extends AsyncTask<String, Void, Bitmap> { private RemoteViews views; public DownloadBitmap(RemoteViews views){ this.views = views; } //..... public void onPostExecute(Bitmap bitmap){ views.setImageViewBitmap(R.id.imageView1, bitmap); } } 
+7
source

See Andy Res solution above. I changed it a bit to pass more parameters ... but it works !!!!

I call it this way:

 new DownloadBitmap(views, appWidgetId, appWidgetManager).execute("MyTestString"); 

then my task starts as follows:

 public class DownloadBitmap extends AsyncTask<String, Void, Bitmap> { /** The url from where to download the image. */ private String url = "http://0.tqn.com/d/webclipart/1/0/5/l/4/floral-icon-5.jpg"; private RemoteViews views; private int WidgetID; private AppWidgetManager WidgetManager; public DownloadBitmap(RemoteViews views, int appWidgetID, AppWidgetManager appWidgetManager){ this.views = views; this.WidgetID = appWidgetID; this.WidgetManager = appWidgetManager; } @Override protected Bitmap doInBackground(String... params) { try { InputStream in = new java.net.URL(url).openStream(); Bitmap bitmap = BitmapFactory.decodeStream(in); Log.v("ImageDownload", "download succeeded"); Log.v("ImageDownload", "Param 0 is: " + params[0]); return bitmap; //NOTE: it is not thread-safe to set the ImageView from inside this method. It must be done in onPostExecute() } catch (Exception e) { Log.e("ImageDownload", "Download failed: " + e.getMessage()); } return null; } @Override protected void onPostExecute(Bitmap bitmap) { if (isCancelled()) { bitmap = null; } views.setImageViewBitmap(R.id.imageView1, bitmap); WidgetManager.updateAppWidget(WidgetID, views); } 
+2
source

Source: https://habr.com/ru/post/1488850/


All Articles