我无法完成如何将图像下载到我的小部件中!
在我的小部件中,我使用url,title加载AsyncTask json,然后我在TextView中显示标题,我需要从url加载图像。
我尝试了这个但图像加载,现在显示
class LoadImages extends AsyncTask<Void, Void, Bitmap>{
@Override
protected Bitmap doInBackground(Void... params) {
Bitmap bitmap = null;
InputStream in = null;
BufferedOutputStream out = null;
try {
in = new BufferedInputStream(new URL("http://mysite/simple.img").openStream());
final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
out = new BufferedOutputStream(dataStream);
out.flush();
final byte[] data = dataStream.toByteArray();
BitmapFactory.Options options = new BitmapFactory.Options();
//options.inSampleSize = 1;
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,options);
Log.e("Log", "Yeah");
} catch (IOException e) {
Log.e("Log", "Could not load Bitmap from: " + "mysiteg");
}
return bitmap;
}
}
在更新中,我称之为
LoadImages load = new LoadImages();
load.execute();
Bitmap bitmap = load.get();
update.setImageViewBitmap(R.id.imageView0, bitmap);
答案 0 :(得分:1)
您应该在LoadImages类中重写onPostExecute方法,并在该方法中将位图设置为ImageView。当LoadImages任务完成后,它将调用onPostExcute方法。考虑使用WeakReference来包装ImageView以获得更好的性能。