笔者认为volley 是android平台上的网络通信库,使网络通信更快,更简单,更健壮,扩展性极强。笔者在项目开发中是用volley框架,但是用volley进行网络图片请求问题还是有不少的,但是又不想在使用其它的图片请求框架,图片请求框架很多如:Picasso,android-universal-image-loader等。

网络图片请求主要注意三点。

1:快速展现图片,特别是listView滑动的时候这个问题在http://blog.csdn.net/wuchuy/article/details/51220523这篇博客有简单分析到。

2:避免内存溢出, 图片及时回收,对图片显示过多的APP,一般都会用调试测试应用查看内存是否存在暴增,视图对BitMap的引用不宜过多,超大的图片压缩处理显示。

3:缓存机制,本地缓存和内存缓存,读取图片先去内存缓存读取,发现没有在去磁盘读取,磁盘没有再去网络读取,这样可以减少对服务器的请求,以及在移动网络情况下避免流量的浪费。

这里我就分析一下volley本缓存导致初始化过慢的原因,volley提供的本地缓存类是DiskBasedCache类,其中一个构造函数DiskBasedCache(File rootDirectory, int maxCacheSizeInBytes) rootDirectory 缓存的根目录,maxCacheSizeInBytes缓存文件的总够的大小,如果用volley进行了图片请求缓存大小我一般设置的比较大,默认是5 * 1024 * 1024;

那么问题来了 我们看 DiskBasedCache 的public synchronized void initialize()方法函数体方法的实现

public synchronized void initialize() {

if (!mRootDirectory.exists()) {

if (!mRootDirectory.mkdirs()) {

VolleyLog.e("Unable to create cache dir %s", mRootDirectory.getAbsolutePath());

}

return;

}

File[] files = mRootDirectory.listFiles();

if (files == null) {

return;

}

for (File file : files) {

BufferedInputStream fis = null;

try {

fis = new BufferedInputStream(new FileInputStream(file));

CacheHeader entry = CacheHeader.readHeader(fis);

entry.size = file.length();

putEntry(entry.key, entry);

} catch (IOException e) {

if (file != null) {

file.delete();

}

} finally {

try {

if (fis != null) {

fis.close();

}

} catch (IOException ignored) { }

}

}

在初始化时文件一口气读取,图片请求后,文件过多过大,初始化会很慢,那么怎么解决呢?

个人是这么解决的,需要读取时才去读取本地的缓存文件,自己实现Volley框架的的Cache的接口写一个类似DiskBasedCache的类,在new 出RequestQueue对象时,构造函数传递自己实现Cache接口的对象,对于源码我们还是不要轻易去修改,Volley有强大的扩展性。

其实这个方法就是空实现

public synchronized void initialize() {

if (!mRootDirectory.exists()) {
if (!mRootDirectory.mkdirs()) {
VolleyLog.e("Unable to create cache dir %s", mRootDirectory.getAbsolutePath());
}
return;
}
}

我对这个get方法加以修改

@Override
public synchronized Entry get(String key) {

CacheHeader entry = mEntries.get(key);
// if the entry does not exist, return.

//获取缓存的文件
File file = getFileForKey(key);
CountingInputStream cis = null;
try {
FileInputStream fis = new FileInputStream(file);
cis = new CountingInputStream(fis);

//如果缓存为空读取后putEntry
if(entry ==null)
{
entry =CacheHeader.readHeader(cis);
entry.size = file.length();
putEntry(entry.key, entry);
}else{
CacheHeader.readHeader(cis); // eat header
}
byte[] data = streamToBytes(cis, (int) (file.length() - cis.bytesRead));
return entry.toCacheEntry(data);
}catch (FileNotFoundException e){//文件不存在的异常处理
return null;
}catch (IOException e) {//出现IO异常移除掉缓存文件,可能文件出错
VolleyLog.d("%s: %s", file.getAbsolutePath(), e.toString());
remove(key);
return null;
} finally {
if (cis != null) {
try {
cis.close();
cis = null;
} catch (IOException ioe) {
return null;
}
}
}

}


OK,经过我这么修改,缓存文件过保存过多后,并不会导致以后APP第一次请求很慢,而且发现用Volley请求图片其实也是很完美的,以上只是笔者的观点,android中有很多优秀的框架值的我们去学习。





更多相关文章

  1. 一款常用的 Squid 日志分析工具
  2. GitHub 标星 8K+!一款开源替代 ls 的工具你值得拥有!
  3. RHEL 6 下 DHCP+TFTP+FTP+PXE+Kickstart 实现无人值守安装
  4. Linux 环境下实战 Rsync 备份工具及配置 rsync+inotify 实时同步
  5. android 开发错误点滴积累--Asset资源管理
  6. Android之获取本地图片并压缩方法
  7. Android基础(一) ImageButton
  8. android 自定义评分
  9. 手动生成Android的R.java文件

随机推荐

  1. android使用HTTP协议读取数据
  2. ImageView
  3. Android Service实现闹钟
  4. android 文档路径获取
  5. Android OTA 升级之二:脚本 ota_from_targ
  6. Android studio 页面布局无法显示问题
  7. Ubuntu amd64 running Android SDKs and
  8. Android tips tool 发现的性能问题
  9. Android软键盘用法实例
  10. Android之SharedPreferences权限