【问题标题】:android - delay while loading Bitmaps into ArrayList using UniversalImageLoaderandroid - 使用 UniversalImageLoader 将位图加载到 ArrayList 时出现延迟
【发布时间】:2015-08-02 22:50:51
【问题描述】:

我需要将 Bitmaps 加载到 ArrayList 中,然后将其转换为 Bitmap[] 并传递给 ArrayAdapter 以扩充 ListView。我使用 UniversalImageLoader 库,这是我的代码:

final ArrayList<Bitmap> imgArray = new ArrayList<>(); //before the method scope, as a class field
//...some code...
File cacheDir = StorageUtils.getOwnCacheDirectory(
            getApplicationContext(),
            "/sdcard/Android/data/random_folder_for_cache");

    DisplayImageOptions options = new DisplayImageOptions.Builder()
            .cacheInMemory(true).cacheOnDisc(true).build();

    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
            getApplicationContext()).defaultDisplayImageOptions(options)
            //.discCache(new FileCounterLimitedCache(cacheDir, 100)) - I commented it 'cause FileCounterLimitedCache isn't recognized for some reason
            .build();
    ImageLoader.getInstance().init(config);

    for (int num=0;num<4;num++) {
        ImageLoader imageLoader = ImageLoader.getInstance();
        final int constNum = num;
        imageLoader.loadImage("http://example.com/sample.jpg", new SimpleImageLoadingListener()
        {
            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage)
            {
                imgArray.add(constNum, loadedImage);
            }
        });
    }

但我有一些问题需要解决。首先,它在第一次运行时经常返回错误(IndexOutOfBoundsException - 当前索引超出 ArrsyList 的大小)。然后过了一段时间(大约一分钟),ArrayList 的大小为 1(我用 Toast 检查它),然后当立即再次运行时,它已经是 4(因为它需要)。奇怪的。但是我需要首先填充 ArrayList 然后完成所有其他操作(它们被延迟并且我在第一次运行时没有错误)。怎么做?

如果有人没有 SD 卡怎么办?顺便说一句,我在 SD 上找不到创建的缓存文件夹...

【问题讨论】:

    标签: android arraylist bitmap delay universal-image-loader


    【解决方案1】:

    您不能以您尝试的方式添加元素。例如,如果第二张图片首先完成加载,您的代码将正确抛出 IndexOutOfBoundsException,因为您尝试添加的位置超出了当前大小 - 请参阅 the documentation

    您最好使用初始化为元素数量的数组,因为您知道它是 4 个元素 - 例如

    final Bitmap[] imgArray = new Bitmap[4];
    

    然后在你的onLoadingComplete() 中添加元素

    imgArray[constNum] = loadedImage;
    

    【讨论】:

    • 其实我不知道会有多少项目,我只是简化了这里的代码并添加了确切的数字。我从服务器解析不同的数据 - 也许可以先解析所有其余数据,然后,当我已经知道所有其他数组的实际大小时,按照您提供的方式进行操作。会好起来吗,你觉得怎么样?
    • 它可以工作,但是我的 ListView 的滚动变得更糟了,我的 Nexus 5 模拟器上仍然没有显示图片(自 API 21 起)。我想解决这个问题这么久还是不知道该怎么办
    【解决方案2】:

    您可以使用SparseArray 而不是ArrayList 来避免IndexOutOfBoundsException。 我想这就是你所追求的:

    final SparseArray<Bitmap> imgArray = new SparseArray<>(); //before the method scope, as a class field
    int numberOfImages;
    int numberOfLoadedImages;
    
    //...some code...
    File cacheDir = StorageUtils.getOwnCacheDirectory(
            getApplicationContext(),
            "/sdcard/Android/data/random_folder_for_cache");
    
    DisplayImageOptions options = new DisplayImageOptions.Builder()
            .cacheInMemory(true).cacheOnDisc(true).build();
    
    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
            getApplicationContext()).defaultDisplayImageOptions(options)
            //.discCache(new FileCounterLimitedCache(cacheDir, 100)) - I commented it 'cause FileCounterLimitedCache isn't recognized for some reason
            .build();
    ImageLoader.getInstance().init(config);
    
    numberOfImages = 4;
    numberOfLoadedImages = 0;
    
    for (int num=0;num<4;num++) {
        ImageLoader imageLoader = ImageLoader.getInstance();
        final int constNum = num;
        imageLoader.loadImage("http://example.com/sample.jpg", new SimpleImageLoadingListener()
        {
            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage)
            {
                imgArray.put(constNum, loadedImage);
                numberOfLoadedImages++;
                if(numberOfImages == numberOfLoadedImages)
                {
                    //Do all the other actions
                }
            }
        });
    }
    

    【讨论】:

      【解决方案3】:

      Universal Image Loader 库包含以下方法:

      public void displayImage(java.lang.String uri, android.widget.ImageView imageView)
      

      可以通过图片 url 和 ImageView 来显示图片。 此外,如果稍后将相同的 ImageView 传递给方法但使用不同的 url,则可能会发生两件事:

      1. 如果旧图已经下载,一般情况下新图会设置到ImageView。

      2. 如果旧图像仍在下载库,将取消正在下载旧图像的 http 连接。并开始下载 新图像。 (可以在 LogCat 中观察到)。这种行为发生 使用适配器时。

      方法调用:

      ImageLoader.getInstance().displayImage("http://hydra-media.cursecdn.com/dota2.gamepedia.com/b/bd/Earthshaker.png", imageView1);
      

      配置:

      如果未指定 defaultDisplayImageOptions,缓存将不起作用。它告诉库将这些图像保存在哪里。因为这个库有从 Assets、Drawables 或 Internet 加载图像的选项:

      DisplayImageOptions opts = new DisplayImageOptions.Builder().cacheInMemory(true).cacheOnDisk(true).build();
      

      使用此选项,图像将保存在应用程序中。内部存储器。 不用担心设备是否有外部存储器。

      ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this)
                      .defaultDisplayImageOptions(opts)
                      .memoryCache(new LruMemoryCache(2 * 1024 * 1024))
                      .memoryCacheSize(2 * 1024 * 1024)
                      .diskCacheSize(50 * 1024 * 1024)
                      .diskCacheFileCount(100)
                      .writeDebugLogs()
                      .build();
      
      ImageLoader.getInstance().init(config);
      

      I created a working github repository for it. check it out.

      【讨论】:

      • 但是如果我有 currentView(ListView 项的视图)而不是 ImageView 来让图像膨胀,它会一样吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-27
      • 1970-01-01
      • 1970-01-01
      • 2011-05-03
      • 1970-01-01
      相关资源
      最近更新 更多