Android下载网络图片到本地
原创
©著作权归作者所有:来自51CTO博客作者风云正的原创作品,请联系作者获取转载授权,否则将追究法律责任
Android下载网络图片到本地sdcard中
1.方法调用:
imageDownload(https://www.baidu.com/img/bdlogo.png);
2.图片下载:
private void imageDownload(final String imgUrl){
final String tempFileName = System.currentTimeMillis() + ".png";
new Thread(new Runnable() {
URL url;
@Override
public void run() {
// TODO Auto-generated method stub
try {
url = new URL(imgUrl);
InputStream is = url.openStream();
Bitmap bitmap = BitmapFactory.decodeStream(is);
saveFile(bitmap, tempFileName);
handler.sendEmptyMessage(GETPIC_OK);
is.close();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
}
3.下载完成,handle处理:
Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
if (msg.what == GETPIC_OK) {
if(!Utils.isEmpty(userImagePath)){
}
}
};
};