首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >位图不缓存吗?

位图不缓存吗?
EN

Stack Overflow用户
提问于 2015-09-11 04:40:02
回答 2查看 27关注 0票数 0

有一个问题,它花了太长的时间从我的web服务器加载个人资料图片。这是我用来加载图像(在AsyncTask中)的代码:

代码语言:javascript
复制
@Override
        protected Bitmap doInBackground(Void... params) {
            String urlPath = AppVariables.SERVER_ADDRESS + "pictures/" + name + ".JPG";
            try {
                URLConnection connection = new URL(urlPath).openConnection();
                connection.setUseCaches(true);
                connection.setConnectTimeout(1000 * 30);
                connection.setReadTimeout(1000 * 30);
                return BitmapFactory.decodeStream((InputStream) connection.getContent(), null, null);
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }

这应该可以根据上面的答案工作:Android image caching

我仍然感觉到延迟--当我进入应用程序中的“资料”时,资料图片需要2-3秒才能显示出来。

有什么想法吗?

EN

回答 2

Stack Overflow用户

发布于 2015-09-11 04:49:28

代码语言:javascript
复制
//onCreate
try
    {
        File httpCacheDir = new File(getApplicationContext().getCacheDir(), "http");
        long httpCacheSize = 100 * 1024 * 1024; // 100 MiB
        HttpResponseCache.install(httpCacheDir, httpCacheSize);
    }
    catch (Exception e) {}

//onStop
try {
        HttpResponseCache cache = HttpResponseCache.getInstalled();
        if (cache != null) {
            cache.flush();
        }
    }
    catch(Exception e){}

您正在设置缓存吗?

票数 0
EN

Stack Overflow用户

发布于 2015-09-11 05:03:37

只需使用通用图像加载器( https://github.com/nostra13/Android-Universal-Image-Loader )即可。

您可以像这样设置缓存图像:

代码语言:javascript
复制
DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
            .cacheOnDisk(true)
            .build();

然后,您可以像这样获得缓存图像的URL:

代码语言:javascript
复制
File file = DiscCacheUtil.findInCache(imageUrl, imageloader.getDiscCache());
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32511211

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档