【发布时间】:2010-07-09 03:46:28
【问题描述】:
我需要从 URL 加载和更新图像。
使用 AsyncTask,我能够从 URL 加载图像 bt 我需要每 10 秒从 url 重新加载图像。
请帮助我如何解决这个问题。
提前致谢
【问题讨论】:
标签: android android-emulator android-widget
我需要从 URL 加载和更新图像。
使用 AsyncTask,我能够从 URL 加载图像 bt 我需要每 10 秒从 url 重新加载图像。
请帮助我如何解决这个问题。
提前致谢
【问题讨论】:
标签: android android-emulator android-widget
@Praveenb 尝试关注,
Bitmap bmImg;
void downloadFile(String fileUrl){
URL myFileUrl =null;
try {
myFileUrl= new URL(fileUrl);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bmImg = BitmapFactory.decodeStream(is);
// it will decode the input stream and will load the bitmat in bmImg variable
imView.setImageBitmap(bmImg);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
【讨论】:
以下代码对我来说很好,
class DownloadImage extends AsyncTask<Void, Void, Drawable>{
@Override
protected Drawable doInBackground(Void... params) {
return Util.getImageFromURL(imageURL);
}
@Override
protected void onPostExecute( Drawable d ) {
getImageIcon().setImageDrawable(d);
}
}
new DownloadImage().execute();
如果你在列表视图中显示图像,你应该遵循这个 http://github.com/commonsguy/cwac-thumbnail
【讨论】: