Android下载网络图片到本地
程序员文章站
2022-06-04 15:31:07
...
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)){ } } }; };
上一篇: 【转载】Linux操作系统下的三种Java环境配置方法
下一篇: Android常用控件技巧总结