Android中保存网络图片到SD卡
2016-02-15 16:24
525 查看
/** * 下载闪屏页的图片 */ private void loadFirstImage(final String url) { /** * 在子线程中开启下载图片的任务 */ new Thread(new Runnable() { @Override public void run() { URL imageUrl; try { if (!TextUtils.isEmpty(url)) { imageUrl = new URL(url); InputStream is = imageUrl.openStream(); Bitmap bitmap = BitmapFactory.decodeStream(is); saveFile(bitmap, System.currentTimeMillis() + ".jpg"); //将当前的时间作为图片的名字保存起来 is.close(); } } catch (Exception ex) { ex.printStackTrace(); } } }).start(); } /** * 保存图片到本地的SD卡 * * @param bitmap 图片对象 * @param fileName 图片名 */ private void saveFile(Bitmap bitmap, String fileName) { try { String filePath = ""; if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) { //判断SD卡是否挂载 filePath = Environment.getExternalStorageDirectory() + "/renruiJob/"; } else { return; } File dirFile = new File(filePath); if (!dirFile.exists()) { dirFile.mkdir(); } File myPictureFile = new File(filePath + fileName); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myPictureFile)); bitmap.compress(Bitmap.CompressFormat.JPEG, 80, bos); bos.flush(); bos.close(); } catch (Exception ex) { ex.printStackTrace(); } }
相关文章推荐
- TCP/IP协议之TCP
- Win10系统无线网络经常掉线问题的解决办法
- TCP协议中的三次握手和四次挥手(图解)
- http之206状态码
- Linux中TCP连接过程状态简介
- Linux服务器上监控网络带宽的18个常用命令
- 网络攻防技术与实践
- 2016百度竞价(SEM)质量度的重要意义,必须了解?
- [Python]网络爬虫4
- RHCE 学习笔记(22) 网络用户
- http 协议
- Android操作HTTP实现与服务器通信
- [Python]网络爬虫3
- Android网络请求好文
- [Python]网络爬虫2
- [Python]网络爬虫1
- iOS开发之网络编程--使用NSURLConnection实现大文件断点续传下载
- TCP/IP协议
- TCP的推送比特PSH(Push)
- 关于电脑开机网络图标一直转圈的问题