上一篇使用了Universal-Image-Loader这个控件加载了网络图片,当然,你觉得这个控件只能加载网络图片的话,那你就太小瞧它了,它同样也能加载本地图片!这次就来使用它加载本地图库的照片。
这次我整合了上次加载网络图片的功能很这次的加载本地图片的功能,放在了一起。
先上效果图(加载网络图片的效果在上一篇博客):
Universal-Image-Loader用来加载图片的方式都是通过传递URl来加载的,这是项目里给出的URl的形式:
"http://site.com/image.png" // from Web
"file:///mnt/sdcard/image.png" // from SD card
"file:///mnt/sdcard/video.mp4" // from SD card (video thumbnail) "content://media/external/images/media/13" // from content provider "content://media/external/video/media/13" // from content provider (video thumbnail) "assets://image.png" // from assets
"drawable://" + R.drawable.img // from drawables (non-9patch images)
依然给出代码:
//得到本地图片的URis
map = getImageUrl();
// 配置图片加载及显示选项(还有一些其他的配置,查阅doc文档吧)
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showStubImage(R.drawable.loading)
// 在ImageView加载过程中显示图片
.showImageForEmptyUri(R.drawable.loading)// image连接地址为空时
.showImageOnFail(R.drawable.loading)// image加载失败
.cacheOnDisc(true)// 加载图片时会在磁盘中加载缓存
.cacheInMemory(true)// 加载图片时会在内存中加载缓存
.build();
// 主要有线程类、缓存大小、磁盘大小、图片下载与解析、日志方面的配置
ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(
getApplicationContext())
.memoryCache(new UsingFreqLimitedMemoryCache(16 * 1024 * 1024))
.defaultDisplayImageOptions(options).build();
ImageLoader.getInstance().init(configuration);
LocalWaterFallAdapter adapter = new LocalWaterFallAdapter(map,
getApplicationContext());
localList.setAdapter(adapter);
}
//得到图片的URl
public HashMap<Integer, Model> getImageUrl() {
HashMap<Integer, Model> datas = new HashMap<Integer, Model>();
Cursor cursor = getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
new String[] { MediaStore.Images.Media.DATA,
MediaStore.Images.Media._ID }, null, null, null);
for (int i = 0; i < cursor.getCount(); i++) {
cursor.moveToPosition(i);
int dataIndextColumn = cursor
.getColumnIndex(MediaStore.Images.Media.DATA);
String fileName = cursor.getString(dataIndextColumn);
try {
File file = new File(fileName);
if (!file.exists()) {
continue;
}
} catch (Exception e) {
// TODO: handle exception
}
Model model = new Model("file:/"
+ cursor.getString(dataIndextColumn).toString());
datas.put(i, model);
}
cursor.close();
return datas;
}
//Model这个类用来加载uri
public class Model {
String url;
public Model(String url) {
this.url = url;
}
}
适配器代码:
private Context context;
private HashMap<Integer, Model> map;
private Drawable drawable;
private ImageLoader imageLoader;
private DisplayImageOptions options;
public LocalWaterFallAdapter(HashMap<Integer, Model> map, Context context) {
this.context = context;
this.map = map;
drawable = context.getResources().getDrawable(R.drawable.loading);
imageLoader = imageLoader.getInstance();
imageLoader.init(ImageLoaderConfiguration.createDefault(context));
options = new DisplayImageOptions.Builder()
.showStubImage(R.drawable.loading)
.showImageForEmptyUri(R.drawable.loading)
.showImageOnFail(R.drawable.loading).cacheInMemory(true)
.bitmapConfig(Bitmap.Config.RGB_565).build();
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return map != null ? map.size() : 0;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final ViewHolder viewHolder;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(
R.layout.image_item, null);
viewHolder = new ViewHolder();
viewHolder.imageView = (ImageView) convertView
.findViewById(R.id.image);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
imageLoader.displayImage(map.get(position).url, viewHolder.imageView,
options);
return convertView;
}
class ViewHolder {
ImageView imageView;
}
这样就能使用Unversial-Image-Loader来加载本地图片了,也是相当方便,学习就到这了,当然还远远不够,真正厉害的大神都是去直接解析源代码的算法之类的。我暂时还看不懂,只能慢慢学吧!总之,Unversial-Image-Loader是一款非常好的用来异步加载图片的工具!