【问题标题】:android - loading Bitmaps into array with UniversalImageLoaderandroid - 使用 UniversalImageLoader 将位图加载到数组中
【发布时间】:2015-08-02 11:57:55
【问题描述】:

我正在尝试用位图填充数组列表,这是我的代码:

ImageLoader imageLoader = ImageLoader.getInstance();
    ArrayList<Bitmap> imgArray = new ArrayList<>();
    for (int num=0;num<4;num++) {
        imgArray.add(num,imageLoader.loadImageSync("http://example.com/sample.jpg"));
    }

但是我得到了 NullPointerException,尽管 loadImageSync(uri) 应该返回 Bitmap,如 GitHub 上的文档中所述。如何解决问题?

【问题讨论】:

  • 你初始化ImageLoader了吗? (ImageLoader.getInstance().init(&lt;CONFIG&gt;);)
  • 不能这样写“CONFIG”。它导致 Bitmap.Config,但在它之后仍然不行
  • 如何初始化配置?请给我一个例子
  • @MarcoBatista 你能帮我初始化配置吗?
  • 对不起,我已经离开了一天,但我看到 hasan83 已经指出了正确的方向 :)

标签: android arraylist bitmap universal-image-loader


【解决方案1】:

配置:

File cacheDir = StorageUtils.getOwnCacheDirectory(
                getApplicationContext(),
                "/sdcard/Android/data/random_folder_name_for_cache");

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

        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
                getApplicationContext()).defaultDisplayImageOptions(options)
                .discCache(new FileCountLimitedDiscCache(cacheDir, 100))
                .build();

        ImageLoader.getInstance().init(config);


final ArrayList<Bitmap> imgArray = new ArrayList<>(); // this should be a public array outside of the method scope. a member of the activity
for (int num=0;num<4;num++) {
    ImageLoader imageLoader = ImageLoader.getInstance(); 
    int final 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);
        }
    });        
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-05-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-06
  • 1970-01-01
相关资源
最近更新 更多