volley获取网络图片,生成本地图片
来源:互联网 发布:图解希尔巴布尔美灭mac 编辑:程序博客网 时间:2024/04/30 09:32
使用volley获取网络图片前先检测是否有本地缓存,有缓存就读取缓存,并生成一个图片保存在本地指定位置(当然也可以访问网络的同时生成本地图片)。自定义一个CustomNetworkImageView继承NetworkImageView实现能够显示本地的Bitmap
RequestQueue mQueue = Volley.newRequestQueue(this);Cache cache = mQueue.getCache();Entry entry = cache.get(url);if (entry!=null) {try {// String data=new String(entry.data,"Utf-8");// 处理data,将其转化为JSON,XML,Bitmap等等Log.e(tag, "缓存存在");String string = Base64.encodeToString(entry.data,Base64.DEFAULT);huancunBitmap = convertStringToIcon(string);networkImageView.setLocalImageBitmap(huancunBitmap);// 把图片保存到本地指定文件夹byte[] byteIcon = Base64.decode(string, Base64.DEFAULT);for (int i = 0; i < byteIcon.length; ++i) {if (byteIcon[i] < 0) {byteIcon[i] += 256;}}// 建立一个文件对象File iconFile = new File(Environment.getExternalStorageDirectory(), "12345.png");FileOutputStream fos = new FileOutputStream(iconFile);if (!iconFile.exists()) {iconFile.createNewFile();}// 把图片数据写入文件形成图片fos.write(byteIcon);} catch (Exception e) {e.printStackTrace();}} else {// 缓存中不存在,做网络请求Log.e(tag, "缓存不存在");imageLoader = new ImageLoader(mQueue, new ImageCache() { @Override public void putBitmap(String url, Bitmap bitmap) { } @Override public Bitmap getBitmap(String url) { return null; } });networkImageView.setDefaultImageResId(R.drawable.default_image);networkImageView.setErrorImageResId(R.drawable.failed_image);networkImageView.setImageUrl(url, imageLoader);}
我把代码上传了一份
0 0
- volley获取网络图片,生成本地图片
- 使用Volley获取网络图片,并作本地缓存
- 使用Volley获取网络图片,并作本地缓存
- volley获取网络图片的一些方法
- 基于volley的获取网络图片
- volley请求网络图片
- Volley加载网络图片
- Volley加载网络图片
- Volley加载网络图片
- Volley加载网络图片
- Volley动态获取图片
- 保存Volley的缓存图片到本地,当网络不通的时候,访问本地的图片
- 保存Volley的缓存图片到本地,当网络不通的时候,访问本地的图片
- volley加载本地图片等
- 使用volley加载本地图片
- 网络爬虫之获取图片到本地
- 获取网络图片,并保存到本地
- JAVA获取网络图片保存本地
- 异步交互请求网络
- 查看物理CPU/内核数/线程数
- 关于 mysql 主从复制的简单配置
- Docker Machine快速安装Docker环境(二)
- 如何同时支持leftBarButtonItem及右滑返回手势
- volley获取网络图片,生成本地图片
- How to handle javascript alerts, confirmation and prompts?
- 本地分区索引如何确定表空间
- bzoj4385 Wilcze doły 单调队列
- 由Illegal access: this web application instance has been stopped already. 引出的tomcat配置问题
- js中怎么样取到本div下面的第一个span里面的值
- 微信公众平台
- 【linux】安装java中可能用到的命令
- iOS时间选择器Demo(-)