有一个问题,它花了太长的时间从我的web服务器加载个人资料图片。这是我用来加载图像(在AsyncTask中)的代码:
@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秒才能显示出来。
有什么想法吗?
发布于 2015-09-11 04:49:28
//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){}您正在设置缓存吗?
发布于 2015-09-11 05:03:37
只需使用通用图像加载器( https://github.com/nostra13/Android-Universal-Image-Loader )即可。
您可以像这样设置缓存图像:
DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
.cacheOnDisk(true)
.build();然后,您可以像这样获得缓存图像的URL:
File file = DiscCacheUtil.findInCache(imageUrl, imageloader.getDiscCache());https://stackoverflow.com/questions/32511211
复制相似问题