我正在尝试使用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上的文档中所述。如何解决问题?
答案 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);
}
});
}