public class GlobalApplication extends Application {
public static ImageLoader imageLoader = ImageLoader.getInstance();
public static DisplayImageOptions options;
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
initImageLoader(getApplicationContext());
options = new DisplayImageOptions.Builder()
.showStubImage(R.drawable.ic_launcher)// 加載等待 時顯示的圖片
.showImageForEmptyUri(R.drawable.ic_launcher)// 加載數據為空時顯示的圖片
.showImageOnFail(R.drawable.ic_launcher)// 加載失敗時顯示的圖片
.cacheInMemory().cacheOnDisc() /**
* .displayer(new
* RoundedBitmapDisplayer(20))
**/
.build();
}
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 config = new ImageLoaderConfiguration.Builder(
context).threadPriority(Thread.NORM_PRIORITY - 2)
.denyCacheImageMultipleSizesInMemory()
//軟引用和強引用結合
//FIFOLimitedMemoryCache(先進先出的緩存策略,當超過設定值,先刪除最先加入緩存的bitmap)
.memoryCache(new FIFOLimitedMemoryCache(2*1024*1024))
// default為使用HASHCODE對UIL進行加密命名, 還可以用MD5(new Md5FileNameGenerator())加密
//.diskCacheFileNameGenerator(new HashCodeFileNameGenerator())
.discCacheSize(50*1024*1024)
.discCacheFileCount(100)
.discCacheFileNameGenerator(new Md5FileNameGenerator())
.tasksProcessingOrder(QueueProcessingType.LIFO).enableLogging() // Not
.build();
// Initialize ImageLoader with configuration.
ImageLoader.getInstance().init(config);
// imageLoader.init(ImageLoaderConfiguration.createDefault(context));
}
}
這是在Application裡的配置
public class HeadFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.headfragment,container,false);
ImageView iv = (ImageView) view.findViewById(R.id.head_iv);
Bundle bundle = getArguments();
String headImageUrl = bundle.getString("headImageUrl");
Log.e("headImageUrl", headImageUrl);
GlobalApplication.imageLoader.displayImage(headImageUrl, iv,GlobalApplication.options);
return view;
}
}
這是用到的時候ImageLoader的時候。
但是老是報錯,怎麼回事啊
你確認你調用了 GlobalApplication的create了嗎,有沒有在mainfest文件中指定這個 GlobalApplication