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

时间:2015-08-02 11:57:56

标签: android arraylist bitmap universal-image-loader

我正在尝试使用Bitmaps填充arraylist,这是我的代码:

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

1 个答案:

答案 0 :(得分: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);
        }
    });        
}