Android-Universal-Image-Loader非常适合加载网络图片,特别是那些使用ListView需要显示很多图片的应用。
但是使用ImageLoader官方demo时,程序跑起来一点问题没有,但是换到自己的项目里确提示java.lang.RuntimeException: ImageLoader must be init with configuration before using
网上已经有解决方案,那就是在Oncreate中添加 imageLoader.init(ImageLoaderConfiguration.createDefault(this));
但是如果是自定义adapter呢,不能直接在Adapter中添加 imageLoader.init(ImageLoaderConfiguration.createDefault(this));进行初始化,否则会提示错误“The method createDefault(Context) in the type ImageLoaderConfiguration is not applicable for the arguments”
这时就要参考ImageLoader官方demo初始化方式,在Application类中进行初始化,然后整个程序都可以直接使用,而不必每次使用时都进行imageLoader初始化。
ImageLoader初始化具体代码,官方demo中UILApplication.java
<span style="font-size:18px;">public static void initImageLoader(Context context) {
// This configuration tuning is custom. You can tune every option, you may tune some of them,
// or you can create default configuration by
// ImageLoaderConfiguration.createDefault(this);
// method.
ImageLoaderConfiguration.Builder config = new ImageLoaderConfiguration.Builder(context);
config.threadPriority(Thread.NORM_PRIORITY - 2);
config.denyCacheImageMultipleSizesInMemory();
config.diskCacheFileNameGenerator(new Md5FileNameGenerator());
config.diskCacheSize(50 * 1024 * 1024); // 50 MiB
config.tasksProcessingOrder(QueueProcessingType.LIFO);
config.writeDebugLogs(); // Remove for release app
// Initialize ImageLoader with configuration.
ImageLoader.getInstance().init(config.build());
}</span>
关于Application参见android Application类的详细介绍