微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – 图像未被缓存在本地(使用通用图像加载程序) – 缓慢的图像加载时间

问题描述:
我正在创建一个可滚动列表的文章,缩略图由我的sqlite数据库填充.一般来说,它是“工作”,除了缓慢:

图像加载非常缓慢…我以为使用“Universal Image Loader”缓存设备上的图像,这将使它们似乎只是滚动到视图,如果你已经查看了(或至少接近).但是 – 当您向上/向下拖动时,图像中没有任何图像,然后3-5秒钟后,图像开始弹出(就像重新下载它们)

我正在更改缩略图框的可见性,但是这是完美无缺的 – 它们似乎没有改变 – 它们只是滚动到视图中,不闪烁或任何东西. (但是图像不会再出现几秒钟).

我已经通过删除我的PHP脚本,滚动后…当我回滚到上一个点,图像不显示 – 让我假设它从我的PHP脚本加载每一次.

但是根据the docs:“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

解决方法

我在列表视图中遇到与图像类似的问题.可能 this answer将纠正您的错误图像问题.

我刚刚使用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正在保留作品.我(很快)重写了作为AsyncTask的LoadAnddisplayImageTask,它立即执行得更好.您可以在GitHub上下载代码的分叉版本.

Universal Image Loader with AsyncTasks

原文地址:https://www.jb51.cc/java/123902.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐