图像未在本地缓存(使用通用图像加载器)-缓慢的图像加载时间
问题内容:
问题描述: 我正在创建一个滚动的带有缩略图的文章列表,该列表由我的SQLite数据库填充。通常,它运行缓慢,但速度较慢:
图像加载非常缓慢…我想使用“ 通用图像加载器 ”将图像缓存在设备上,如果您已经查看过图像(或者至少接近该图像,那会使它们看起来只是滚动到视图中) )。但是-
当您向上/向下拖动时,没有任何图像,然后在3-5秒后,图像开始弹出(就像重新下载它们一样)
我正在动态更改缩略图框的可见性,但是这种方法运行得很完美-它们似乎没有变化-
它们只是滚动到视图中或不滚动,没有闪烁或其他任何东西。(但是随后几秒钟内这些图像才会出现)。
我已经测试过,在滚动后删除了我的PHP脚本…当我滚动回到上一个位置时,图像不会显示-让我假设它每次都从我的PHP脚本中加载。
但是根据 文档: “
UsingFreqLimitedMemoryCache(当超过缓存大小限制时,删除最不常用的位图)-默认情况下使用”
细节:
在我的ArticleEntryAdapter.js
我有:
@Override
public View getView(final int position, final View convertView, final ViewGroup parent) {
// We need to get the best view (re-used if possible) and then
// retrieve its corresponding ViewHolder, which optimizes lookup efficiency
final View view = getWorkingView(convertView);
final ViewHolder viewHolder = getViewHolder(view);
final Article article = getItem(position);
// Set the title
viewHolder.titleView.setText(article.title);
//Set the subtitle (subhead) or description
if(article.subtitle != null)
{
viewHolder.subTitleView.setText(article.subtitle);
}
else if(article.description != null)
{
viewHolder.subTitleView.setText(article.description);
}
ImageLoader imageLoader = ImageLoader.getInstance();
imageLoader.displayImage("", viewHolder.thumbView); //clears previous one
if(article.filepath != null && article.filepath.length() != 0) {
imageLoader.displayImage(
"http://img.sltdb.com/processes/resize.php?image=" + article.filepath + "&size=100&quality=70",
viewHolder.thumbView
);
viewHolder.thumbView.setVisibility(View.VISIBLE);
} else {
viewHolder.thumbView.setVisibility(View.GONE);
}
return view;
}
至于图像不正确-很少,但有时在滚动时,我会看到2张相同的图像,当我看这些文章时,它们根本不相关(即没有机会真正拥有同一张图片)所以-
我向后滚动,然后往回滚动,它不再是错误的图片。
注意:我是Java / Android的新手-您可能已经注意到了。
每个注释请求的更多代码:
private View getWorkingView(final View convertView) {
// The workingView is basically just the convertView re-used if possible
// or inflated new if not possible
View workingView = null;
if(null == convertView) {
final Context context = getContext();
final LayoutInflater inflater = (LayoutInflater)context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
workingView = inflater.inflate(articleItemLayoutResource, null);
} else {
workingView = convertView;
}
return workingView;
}
更新: 我的清单文件具有:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
但是我发现的缓存文件夹是完全空的:
mnt
-sdcard
-Android
-data
-com.mysite.news
-cache
-uil-images
问题答案:
我在列表视图中遇到了类似的图像问题。此答案可能会纠正您的错误图像问题。
我刚刚使用UniversalImageLoader下载了示例项目,它表现出与您描述的相同的行为。
到目前为止,要注意一些源代码。
public static final int DEFAULT_THREAD_POOL_SIZE = 3;
public static final int DEFAULT_THREAD_PRIORITY = Thread.NORM_PRIORITY - 1;
public static final int DEFAULT_MEMORY_CACHE_SIZE = 2 * 1024 * 1024; // bytes
这表示在任何时候都将有三个线程下载,最大2MB的图像。您要下载的图片有多大?您还在缓存到磁盘吗?如果是这样,那将会很慢。
要在ImageLoader中配置一些基本选项,您将需要传递给displayImage:
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showStubImage(R.drawable.stub_image)
.cacheInMemory()
.cacheOnDisc()
.build();
我也希望您尝试以下选项:
ImageLoaderConfiguration imageLoaderConfiguration = new ImageLoaderConfiguration.Builder(this)
.enableLogging()
.memoryCacheSize(41943040)
.discCacheSize(104857600)
.threadPoolSize(10)
.build();
imageLoader = ImageLoader.getInstance();
imageLoader.init(imageLoaderConfiguration);
经过我的测试,图像在磁盘上,但是加载仍然很慢。
经过广泛的测试,我确定了主要问题是UniversalImageLoader速度很慢。具体来说,ImageLoader和LoadAndDisplayImageTask阻止了工作。我(很快)将LoadAndDisplayImageTask重写为AsyncTask,它立即表现更好。您可以在GitHub上下载代码的分支版本。