android volley 框架图片请求后有时会导致第一次初始化过慢的原因

   笔者认为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中有很多优秀的框架值的我们去学习。





评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值