从互联网的图像更新的Android窗口小部件(使用异步任务) [英] update android widget (using async task) with an image from the internet
问题描述
我有,我想从互联网的图像更新一个简单的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?
}
}
我觉得我的code已经成功地从互联网上下载的图像。
I think my code has successfully downloaded the image from the internet.
我很困惑的事情,是如何获得这个图像分成从我的异步任务类的ImageView的特定部件的。要更新的形象,你需要访问3个不同的对象:语境,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窗口小部件(使用异步任务)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!