在我的項目中,我有一個包含圖像的網格視圖。基於我的研究,Universal Image Loader項目旨在在後臺下載圖像。然後基於示例我設置了我的適配器。這是我寫的代碼:Android,如何實例化Universal Image Loader?
package cam.astro.mania.adapters;
import java.io.File;
import java.util.ArrayList;
import com.astro.mania.activities.Contestants_Photo;
import com.astro.mania.activities.R;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.cache.memory.impl.UsingFreqLimitedMemoryCache;
import com.nostra13.universalimageloader.core.DecodingType;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.ImageLoadingListener;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Environment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
public class ContestantsPhotoAdapter extends BaseAdapter {
private Context context;
private LayoutInflater myInflater;
private Bitmap[] bitmapList;
private Bitmap bitmap;
private ArrayList<String> ListOfURLs;
private ImageLoader imageLoader;
private ProgressDialog progressBar;
private File cacheDir;
private ImageLoaderConfiguration config;
private DisplayImageOptions options;
public ContestantsPhotoAdapter(Context c) {
context = c;
myInflater = LayoutInflater.from(c);
// Get singleton instance of ImageLoader
imageLoader = ImageLoader.getInstance();
}
public void setImageURLs(ArrayList<String> list){
ListOfURLs = list;
for(String str: ListOfURLs)
Log.i("URL Address>>>>", str);
cacheDir = new File(Environment.getExternalStorageDirectory(), "UniversalImageLoader/Cache");
// Create configuration for ImageLoader
config = new ImageLoaderConfiguration.Builder(context)
.maxImageWidthForMemoryCache(800)
.maxImageHeightForMemoryCache(800)
.httpConnectTimeout(5000)
.httpReadTimeout(30000)
.threadPoolSize(5)
.threadPriority(Thread.MIN_PRIORITY + 2)
.denyCacheImageMultipleSizesInMemory()
.memoryCache(new UsingFreqLimitedMemoryCache(2000000)) // You can pass your own memory cache implementation
.discCache(new UnlimitedDiscCache(cacheDir)) // You can pass your own disc cache implementation
.defaultDisplayImageOptions(DisplayImageOptions.createSimple())
.build();
// Creates display image options for custom display task
options = new DisplayImageOptions.Builder()
.showStubImage(R.drawable.icon_loading)
.showImageForEmptyUrl(R.drawable.icon_remove)
.cacheInMemory()
.cacheOnDisc()
.decodingType(DecodingType.MEMORY_SAVING)
.build();
// Initialize ImageLoader with created configuration. Do it once.
imageLoader.init(config);
}
@Override
public int getCount() {
return ListOfURLs.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent){
ViewHolder holder;
if (convertView == null) {
convertView = myInflater.inflate(R.layout.grid_contestantsphoto, null);
holder = new ViewHolder();
holder.ivIcon = (ImageView) convertView.findViewById(R.id.imvContestantsPhoto_icon);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
// holder.ivIcon.setImageBitmap(bitmapList[position]);
String imageUrl = ListOfURLs.get(position);
// Load and display image
imageLoader.displayImage(imageUrl, holder.ivIcon, options, new ImageLoadingListener() {
@Override
public void onLoadingStarted() {
showLoading();
}
@Override
public void onLoadingFailed() {
stopLoading();
}
@Override
public void onLoadingComplete() {
stopLoading();
}
});
return convertView;
}
static class ViewHolder {
ImageView ivIcon;
}
/*-----------------------------------------------------------------------------------
* Showing/Stopping progress dialog which is showing loading animation
* ---------------------------------------------------------------------------------*/
private void showLoading(){
progressBar = ProgressDialog.show(context, "", "");
progressBar.setContentView(R.layout.anim_loading);
progressBar.setCancelable(true);
final ImageView imageView = (ImageView) progressBar.findViewById(R.id.blankImageView);
Animation rotation = AnimationUtils.loadAnimation(context, R.anim.rotate);
imageView.startAnimation(rotation);
}
private void stopLoading() {
if(progressBar.isShowing())
progressBar.dismiss();
}
}
我做了什麼? 1)我下載universal-image-loader-1.2.3.jar,放入MY_PROJECT/lib文件夾,然後我加入這個jar文件到Java構建路徑
2)由於兌現的圖像,這個庫需要能夠訪問到本地存儲,所以我加要清單文件<uses-permission android:name = "android.permission.WRITE_EXTERNAL_STORAGE"/>
。
3)現在,當我運行時,應用程序崩潰並指向imageLoader = ImageLoader.getInstance();
。 logcat的消息是:
dalvikvm: Could not find class 'cam.astro.mania.adapters.ContestantsPhotoAdapter$1', referenced from method cam.astro.mania.adapters.ContestantsPhotoAdapter.getView
AndroidRuntime: java.lang.NoClassDefFoundError: com.nostra13.universalimageloader.core.ImageLoader
AndroidRuntime: at cam.astro.mania.adapters.ContestantsPhotoAdapter.<init>(ContestantsPhotoAdapter.java:50)
根據我的研究(例如here),我發現,當Java虛擬機無法找到在運行時一個特定的類,它在編譯時可用此消息「來。「
'你現在用哪個版本的ADT?' – 2012-04-21 09:19:03
謝謝Samir,我不確定,我怎麼能得到它? – Hesam 2012-04-21 09:22:37
[見這裏](http://stackoverflow.com/a/9833655/420015) – adneal 2012-04-21 09:31:33