您的位置:首页 > 移动开发 > Android开发

android简单打图片下载帮忙看看有那些补充打

2017-07-20 13:30 190 查看
public class NetPhotoDown extends Thread {
private Bitmap  bitmap;
private   String  url;
private ImageView img;
public NetPhotoDown(String  url, ImageView img){
this.url=url;
this.img=img;
}
@Override
public void run() {
bitmap=returnBitmap(url);
if (bitmap!=null){
LogShow.soutShow("bitmap!=null");
handler.sendEmptyMessage(downImg);
}else {

handler.sendEmptyMessage(downErrorImg);
}
}
public void  downPhoto(){
this.start();
LogShow.soutShow("start");

}
private final  int downImg=0x888;
private final  int downErrorImg=0x01;
private Handler  handler =new Handler(Looper.getMainLooper()){
@Override
public void handleMessage(Message msg) {
img.setImageBitmap(bitmap);
}
};
public Bitmap returnBitmap(String url) {
URL fileUrl = null;
Bitmap bitmap = null;
try {
fileUrl = new URL(url);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
HttpURLConnection conn = (HttpURLConnection) fileUrl
.openConnection();
conn.setDoInput(true);
conn.connect();
if (conn.getResponseCode()==200){
InputStream is = conn.getInputStream();
bitmap = BitmapFactory.decodeStream(is);
is.close();
}
} catch (IOException e) {
e.printStackTrace();
bitmap=null;
}
return bitmap;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: