protected Bitmap doInBackground(String...params) {
// TODO Auto-generated method stub
try {
/* Open a new URL and get the InputStream to load data from it. */
URL aURL = new URL(params[0]);
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
/* Buffered is always good for a performance plus. */
BufferedInputStream bis = new BufferedInputStream(is);
/* Decode url-data to a bitmap. */
Bitmap bm = BitmapFactory.decodeStream(bis);
b1=bm;
bis.close();
is.close();
} catch (IOException e)
{
Log.e("DEBUGTAG", "Remote Image Exception", e);
}
return null;
}
// TODO Auto-generated method stub
super.onPostExecute(result);
i1.setImageBitmap(b1);
Animation rotation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.rotate);
i1.startAnimation(rotation);
//System.out.println("imageview is working");
try {
System.out.println("thread going to sleep");
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
i1.setImageBitmap(b1);
}
我正在通过位图显示来自Internet的图像,现在我使用了另一个图像,通过更新URL,但我无法显示此图像仅显示上一个图像,如果我运行这仅在循环中,最后一个图像是在示意吗?有任何建议
从您的on treate或您想从网络下载代码
的地方new Thread(new Runnable() {
public void run() {
Drawable drawable = createDrawableFromURL(_mDrawingURL);
Message msgURL = new Message();
msgURL.arg2 = URL_CONSTANT;
msgURL.obj = drawable;
_handler_url.sendMessage(msgURL);
}
}).start();
} catch (JSONException e) {
e.printStackTrace();
}
}
此方法将用于从Internet下载图像:
private Drawable createDrawableFromURL(String urlString) {
Drawable image = null;
try
{
InputStream inputStream = new URL(urlString).openStream();
image = Drawable.createFromStream(inputStream, null);
inputStream.close();
}
catch (MalformedURLException e) {
e.printStackTrace();
image = null;
} catch (IOException e) {
e.printStackTrace();
image = null;
}
return image;
}
下载图像后,图像视图将使用图像进行更新。:
private Handler _handler_url = new Handler(){
public void dispatchMessage(Message msg) {
switch (msg.arg2) {
case URL_CONSTANT:
Drawable drawable = (Drawable) msg.obj;
if(drawable!=null)
_mImageView.setImageDrawable(drawable);
break;
default:
break;
}
};
};