Android,如何实例化通用图像加载器?
Posted
技术标签:
【中文标题】Android,如何实例化通用图像加载器?【英文标题】:Android, How to instantiate Universal Image Loader? 【发布时间】:2012-04-21 09:15:13 【问题描述】:在我的项目中,我有一个包含图像的网格视图。根据我的研究,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 虚拟机无法在运行时找到在编译期间可用的特定类时。 "
【问题讨论】:
which version of ADT are you using now ?
谢谢 Samir,我不确定,我怎样才能得到它?
See here
感谢您提供有用的信息。我的 ADT 版本是 18。当我将 jar 添加到 Java 构建路径并且其文件夹名称为“lib”时,我遇到了上述问题。在这种情况下,我可以运行提到的崩溃问题的应用程序。
当我从 Java 类路径中删除 jar 文件并将文件夹名称从“lib”更改为“libs”时,jar 文件会自动添加到 Android 依赖项。现在,一切都很好:) 非常感谢您的帮助
【参考方案1】:
当我从 Java 类路径中删除 jar 文件并将文件夹名称从“lib”更改为“libs”时,jar 文件会自动添加到 Android 依赖项。现在,一切都很好
【讨论】:
【参考方案2】:添加外部 jar 后,请务必检查“订购和导出”选项卡下的 jar。
【讨论】:
以上是关于Android,如何实例化通用图像加载器?的主要内容,如果未能解决你的问题,请参考以下文章