Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

update android widget (using async task) with an image from the internet

I have a simple android widget that I want to update with an image from the internet. I can display static images on the widget no problem. I am told that you need to use an async task for this and I don't have much experience with these.

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 which does the downloading. 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 has successfully downloaded the image from the internet.

The thing I'm confused about, is how do I get this image into the "ImageView" of the specific widget from my Async task class. To update the image, you need access to 3 different objects: Context, AppWidgetManager, and AppWidgetId.... But how do I pass all of those objects inside this statement:???

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

Thanks!

like image 828
user952342 Avatar asked Jun 29 '13 12:06

user952342


2 Answers

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

In onUpdate():

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

and in the 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);
    }
}
like image 175
Andy Res Avatar answered Oct 21 '22 12:10

Andy Res


Please see Andy Res's solution above. I tweaked it ever so slightly to pass more parameters... but it works!!!!

I call it like this:

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

then, my task starts 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"; 

    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);
} 
like image 41
user952342 Avatar answered Oct 21 '22 13:10

user952342