android 加载网络数据(3)
2016-06-29 15:18
519 查看
加载网络数据
加载网络数据
public class ImageCacheUtils { private LruCache<String, Bitmap> lruCache; private File dirFile; public void init(){ long max = Runtime.getRuntime().maxMemory(); int maxSize =(int)max/6; lruCache = new LruCache<String, Bitmap>(maxSize){ protected int sizeOf(String key, Bitmap value) { return value.getRowBytes()*value.getHeight(); } }; } public Bitmap ImageLoad(final String path,final ImageView iv){ //先去缓存中去 查找----》本地----》网络 Bitmap b1=getBitMapFromLru(path); if(b1!=null){ return b1; } //从本地取出 Bitmap b2=getBitMapFromDir(path); if(b2!=null){ return b2; } Bitmap b3=getBitmapFromNet(path); if(b3!=null){ return b3; } return null; } /** * 缓存中查找 */ public Bitmap getBitMapFromLru(String path){ return lruCache.get(path); } /** * 从本地查找 * http:..../xx.jpg */ public Bitmap getBitMapFromDir(String path){ File file = Environment.getDownloadCacheDirectory(); File f = new File(file,path); Bitmap bitmap = BitmapFactory.decodeFile(f.getPath()); if(bitmap!=null){ //在本地存在 lruCache.put(path, bitmap); return bitmap; } return null; } /** * 从网络中下载 */ public Bitmap getBitmapFromNet(String path){ try { URL url = new URL(path); URLConnection connection =url.openConnection(); InputStream is = connection.getInputStream(); Bitmap bitmap = BitmapFactory.decodeStream(is); if(bitmap!=null){ //1.缓存 2.本地 lruCache.put(path, bitmap); OutputStream os = new FileOutputStream(path); int len=0; byte[] by=new byte[1024]; while((len=is.read(by))!=-1){ os.write(by,0,len); } return bitmap; } } catch (Exception e) { e.printStackTrace(); } return null; } }
加载网络数据
相关文章推荐
- HttpProxyUtil HTTP请求工具类源码
- android 加载网络数据(2)图片
- network interface card 网络接口卡
- 互联网开发2-HTTPS通信原理
- Swift网络封装库Moya中文手册之RxSwift
- libevent学习笔记 - 0 Preliminaries
- java网络编程:RandomAccessFile, URLConnection和多线程机制实现了Http下载
- 获取网络图片,并保存到本地
- 传智播客网络营销课程大升级,改变从“薪”开始
- 经典的BPN带动量的误差反向传播神经网络类代码示例
- java网络编程: UDP 广播与多播的实现
- node.js http
- AutoCAD单机版、网络版授权方法
- java网络编程:MulticastSocket类
- R-FCN:基于区域的全卷积网络来检测物体
- java网络编程:DatagramSocket类简单实现文件下载
- java网络编程:DatagramSocket类
- Swift网络封装库Moya中文手册之Targets
- HTTP状态码及其含义
- Unity判断网络是否连接 以及 判断是否连接WiFi