如何使用Android的CacheManager的?如何使用、Android、CacheManager

2023-09-06 01:15:17 作者:缺锌缺钙缺你爱

我目前正在开发的Andr​​oid应用程序获取使用HTTP请求的图像。这将是非常肿胀,如果我能,以提高性能和带宽使用缓存的图像。

我碰到在Android引用的CacheManager类,但我真的不知道如何使用它,或者它确实做到了。

我已经通过这个例子范围,但我需要一些帮助理解它:

/core/java/android/webkit/gears/ApacheHtt$p$pquestAndroid.java

另外,参考状态:

  

网络请求被提供到该组件,如果他们不能由高速缓存解决,HTTP头所​​连接酌情针对内容重新验证的请求。

我不知道这意味着什么,或者它如何工作对我来说,因为CacheManager的的getCacheFile只接受一个字符串的URL和包含头文件的映射。不知道什么是附件中提到的手段。

这是解释或简单的code例子就是真正做到我的一天。谢谢!

更新的

这就是我现在所拥有的。我清楚地做错了,只是不知道在哪里。

公共静态位图getRemoteImage(字符串IMAGEURL){
        URL aURL = NULL;
        URLConnection的康恩= NULL;
        BMP位= NULL;

        CacheResult cache_result = CacheManager.getCacheFile(IMAGEURL,新的HashMap());

        如果(cache_result == NULL){
            尝试 {
                aURL =新的URL(IMAGEURL);
                康恩= aURL.openConnection();
                conn.connect();
                InputStream的是= conn.getInputStream();

                cache_result =新CacheManager.CacheResult();
                copyStream(是,cache_result.getOutputStream());
                CacheManager.saveCacheFile(IMAGEURL,cache_result);
            }赶上(例外五){
                返回null;
            }
        }

        BMP = BitmapFactory.de codeStream(cache_result.getInputStream());
        返回BMP;
    }

解决方案

我不认为CacheManger可以在web视图之外被用作此bug报告中指出 http://$c$c.google.com/p/android/issues/detail?id=7222

轻量版的原生 Android 好用吗 我用自己的手机体验了 Android Go

I'm currently developing an Android application that fetches images using http requests. It would be quite swell if I could cache those images in order to improve to performance and bandwidth use.

I came across the CacheManager class in the Android reference, but I don't really know how to use it, or what it really does.

I already scoped through this example, but I need some help understanding it:

/core/java/android/webkit/gears/ApacheHttpRequestAndroid.java

Also, the reference states:

"Network requests are provided to this component and if they can not be resolved by the cache, the HTTP headers are attached, as appropriate, to the request for revalidation of content."

I'm not sure what this means or how it would work for me, since CacheManager's getCacheFile accepts only a String URL and a Map containing the headers. Not sure what the attachment mentioned means.

An explanation or a simple code example would really do my day. Thanks!

Update

Here's what I have right now. I am clearly doing it wrong, just don't know where.

public static Bitmap getRemoteImage(String imageUrl) {
        URL aURL = null;
        URLConnection conn = null;
        Bitmap bmp = null;

        CacheResult cache_result = CacheManager.getCacheFile(imageUrl, new HashMap());

        if (cache_result == null) {
            try {
                aURL = new URL(imageUrl);
                conn = aURL.openConnection();
                conn.connect();
                InputStream is = conn.getInputStream();

                cache_result = new CacheManager.CacheResult();
                copyStream(is, cache_result.getOutputStream());
                CacheManager.saveCacheFile(imageUrl, cache_result);
            } catch (Exception e) {
                return null;
            }
        }

        bmp = BitmapFactory.decodeStream(cache_result.getInputStream());
        return bmp;
    }

解决方案

I don't think the CacheManger can be used outside of a WebView as noted in this bug report http://code.google.com/p/android/issues/detail?id=7222