一、当前更新Activity
// handler声明
private Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
imag_mlzx.setScaleType(ScaleType.CENTER_CROP);
imag_mlzx.setImageBitmap(bitmap);
break;
}
};
};
二、oncreat()方法中实现调用
@Override
public void run() {
try {
bitmap = PrcFromUrl
.getBitmap("http://employee.pzfw.net/Image/lunbo/1.jpg");
Message message = new Message();
message.what = 1;
mHandler.sendMessage(message);
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
这是最基本的 给个图片链接就能更新Ui
第二种:创建一个工具类才用Bitmap 进行更新
public class PrcFromUrl {
public static Bitmap getBitmap(String path) throws IOException {
URL fileUrl = null;
Bitmap bitmap = null;
try {
fileUrl = new URL(path);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
HttpURLConnection conn = (HttpURLConnection) fileUrl
.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bitmap = BitmapFactory.decodeStream(is);
is.close();
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
}