public static Bitmap getImage(String Url) throws Exception {
try {
URL url = new URL(Url);
String responseCode = url.openConnection().getHeaderField(0);
if (responseCode.indexOf("200") < 0)
throw new Exception(" 图片文件不存在或路径错误,错误代码: " + responseCode);
return BitmapFactory.decodeStream(url.openStream());
} catch (IOException e) {
// TODO Auto-generated catch block
throw new Exception(e.getMessage());
}
}
此类返回的是一个 Bitmap 的对象,
下面是对类的一个简单调用:
直接上代码:
Bitmap mBitmap ;
// 直接获取图片:
private void RefreshDB() {
try {
sendMSG(Declare.START, " 正在加载图片 ");
mBitmap = DownFile.getImage( 图片地址 );
sendMSG(Declare.STOP, "");
} catch (Exception e) {
// TODO Auto-generated catch block
sendMSG(Declare.ERROR, e.getMessage());
}
这里带有一个简单的滚动条, 表示当前程序正在执行, 通过发送消息的方式实现的, 具
体不再写了,那个消息无头此核心代码的实现 .
在加载完成后执行:
mImageView1.setImageBitmap(mBitmap);
更多信息请查看 IT 技术专栏