android - 使用 UniversalImageLoader 将位图加载到数组中

android - loading Bitmaps into array with UniversalImageLoader

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

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) 应该已经返回位图,如 GitHub 上的文档中所述。如何解决问题?

配置:

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);
        }
    });        
}