gpt4 book ai didi

android - Android的CacheManager如何使用?

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:04:12 28 4
gpt4 key购买 nike

我目前正在开发一个使用 http 请求获取图像的 Android 应用程序。如果我可以缓存这些图像以提高性能和带宽使用,那就太好了。

我在 Android 引用资料中看到了 CacheManager 类,但我真的不知道如何使用它,也不知道它到底做了什么。

我已经了解了这个例子,但我需要一些帮助来理解它:

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

此外,引用说明:

"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."

我不确定这意味着什么或者它对我有何作用,因为 CacheManager 的 getCacheFile 只接受一个字符串 URL 和一个包含 header 的映射。不确定提到的附件是什么意思。

一个解释或一个简单的代码示例真的很适合我。谢谢!

更新

这是我现在拥有的。我显然做错了,只是不知道在哪里。

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;    }

最佳答案

我不认为 CacheManger 可以在 WebView 之外使用,如该错误报告中所述 http://code.google.com/p/android/issues/detail?id=7222

关于android - Android的CacheManager如何使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2059831/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com