使用来自互联网的图像更新 android 小部件(使用异步任务)

update android widget (using async task) with an image from the internet(使用来自互联网的图像更新 android 小部件(使用异步任务))

本文介绍了使用来自互联网的图像更新 android 小部件(使用异步任务)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的 android 小部件,我想用来自互联网的图像进行更新.我可以在小部件上显示静态图像没问题.有人告诉我,您需要为此使用异步任务,而我对这些没有太多经验.

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.

这是我的小部件:

    @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.

我感到困惑的是,如何从我的异步任务类中将此图像放入特定小部件的ImageView"中.要更新图像,您需要访问 3 个不同的对象:Context、AppWidgetManager 和 AppWidgetId.... 但是如何在此语句中传递所有这些对象:???

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");

谢谢!

推荐答案

一种解决方案是将 RemoteViews 作为参数传递给 DownloadBitmap 构造函数,并在 onPostExecute() 设置图片:

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

在 onUpdate() 中:

In onUpdate():

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

在 DownloadBitmap 中:

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);
    }
}

这篇关于使用来自互联网的图像更新 android 小部件(使用异步任务)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:使用来自互联网的图像更新 android 小部件(使用异步任务)

基础教程推荐