带缓存功能的图片查看器
2016-09-07 16:08
176 查看
public void loadImg() { final String path = "http://8080:www.baidu.com/helle.jpg"; final File file = new File(getCacheDir(), getFileName(path)); //首先判断文件缓存是否存在 if (file.exists()) { //通过文件的绝对路径构造Bitmap对象 Bitmap bp = BitmapFactory.decodeFile(file.getAbsolutePath()); ImageView iv = new ImageView(this); iv.setImageBitmap(bp); } else { Thread thread = new Thread() { @Override public void run() { try { //1、获取URL对象 URL url = new URL(path); //2、获取连接对象 HttpURLConnection conn = (HttpURLConnection) url .openConnection(); //3、设置连接方法 conn.setRequestMethod("GET"); //读取超时 conn.setReadTimeout(5000); //连接超时 conn.setConnectTimeout(5000); //4、和服务器建立连接 conn.connect(); if (conn.getResponseCode() == 200) { //5、拿到服务器返回的流 InputStream in = conn.getInputStream(); //6、开启文件输出流,把读取到的流写到本地 FileOutputStream fos = new FileOutputStream(file); int len = 0; byte[] b = new byte[1024]; while ((len = in.read(b)) != -1) { fos.write(b, 0, len); } fos.close(); Bitmap bp = BitmapFactory.decodeFile(file .getAbsolutePath()); // Bitmap bp = BitmapFactory.decodeStream(in); // ImageView iv=new ImageView(getContext()); // iv.setImageBitmap(bp); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }; thread.start(); } }
相关文章推荐
- Android客户端读取PHP服务器的图片等信息,并用ListView列出来,实现缓存功能。
- iOS相册图片缓存(document)以及图片文件流上传服务器功能
- Android之---RecycleView实现简单的图片查看功能(类似相册查看器)
- Android中ViewPager常用功能4补充----viewpager图片查看器
- Android中ViewPager常用功能4----图片查看器
- 带有缩放功能的图片查看器
- 几行代码接入启动页广告, 自带图片下载、缓存相关功能, 无任何第三方依赖 - 支持静态/动态、全屏/半屏广告、iPhone/iPad.
- Android简易实战教程--第二十六话《网络图片查看器在本地缓存》
- 010_02带缓存的图片查看器
- Androidx学习笔记(30)--- 图片下载器加入缓存图片的功能
- 从网络下载的图片加缓存功能
- 二级缓存加载图片实现照片墙功能
- Android实现从缓存中读取图片与异步加载功能类
- WinXP使用图片和传真查看器功能在一张纸上打印多幅图片
- 【Android网络编程】获取网络图片,具有缓存功能
- 带缓存的网页图片查看器
- 一. Volley简介 Volley的中文翻译为“齐射、并发”,是在2013年的Google大会上发布的一款Android平台网络通信库,具有网络请求的处理、小图片的异步加载和缓存等功能,能够帮助
- Android简易实战教程--第二十六话《网络图片查看器在本地缓存》
- 24.带缓存的图片查看器
- 分享一款强大的图片查看器插件,手机PC 通吃,功能超级齐全!