[英]Android: Widget load images
我無法確定如何將圖像下載到我的小部件!
在我的小部件中,我使用網址,標題加載AsyncTask json,然后在TextView中顯示標題,並且需要從網址加載圖像。
我嘗試過這個但圖像加載,現在顯示
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);
您應該重寫LoadImages類中的onPostExecute方法,並在該方法中將位圖設置為ImageView。 完成LoadImages任務后,它將調用onPostExcute方法。 考慮使用WeakReference來包裝ImageView以獲得更好的性能。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.