您的位置:首页 > 其它

如何使用Universal Image Loader加载本地图片

2014-08-21 11:03 549 查看
本篇通过使用UIL(Universal Image Loader)加载本地图片,先上图:



这里使用GridView布局,通过getContentResolver获取手机保存的图片:

/**
* 获取本地图片
*
* @param context
* @return
*/
@SuppressLint("UseSparseArrays")
public static HashMap<Integer, Model> getMediaImage(Context context) {
HashMap<Integer, Model> datas = new HashMap<Integer, Model>();
final String orderBy = MediaStore.Images.Media.DATE_TAKEN;
final String[] columns = { MediaStore.Images.Media.DATA,
MediaStore.Images.Media._ID,
MediaStore.Images.Media.BUCKET_DISPLAY_NAME };
Cursor imagecursor = context.getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null,
null, orderBy + " DESC");

for (int i = 0; i < imagecursor.getCount(); i++) {
imagecursor.moveToPosition(i);
int dataColumnIndex = imagecursor
.getColumnIndex(MediaStore.Images.Media.DATA);
int dirColumnIndex = imagecursor
.getColumnIndex(MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
String buckedName = imagecursor.getString(dirColumnIndex);
Log.v("image", "buckedName = " + buckedName);
String filename = imagecursor.getString(dataColumnIndex);
try {
File file = new File(filename);
if (!file.exists()) {
continue;
}
} catch (Exception e) {
continue;
}

Log.v("image", "filename = " + filename);
Model galleryModel = new Model("file:/" + imagecursor.getString(dataColumnIndex).toString(), false);// false是为了解决checkbox错乱的问题
datas.put(i, galleryModel);
}
imagecursor.close();
return datas;
}


适配器:

public class MyListAdapter extends BaseAdapter {

private Context context;
private HashMap<Integer, Model> datas;
private ImageLoader imageLoader;
private DisplayImageOptions options;

public MyListAdapter(Context context, HashMap<Integer, Model> datas){
this.context = context;
this.datas = datas;

imageLoader = ImageLoader.getInstance();
imageLoader.init(ImageLoaderConfiguration.createDefault(context));

options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.ic_stub)
.showImageForEmptyUri(R.drawable.ic_empty)
.showImageOnFail(R.drawable.ic_error)
.cacheInMemory(true)
.cacheOnDisk(true)
.considerExifParams(true)
.bitmapConfig(Bitmap.Config.RGB_565)
.build();
}

@Override
public int getCount() {
return datas.size();
}

@Override
public Object getItem(int position) {
return position;
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
final ViewHolder holder;
if(convertView == null){
view = LayoutInflater.from(context).inflate(R.layout.listview_item, parent , false);
holder = new ViewHolder();
holder.image = (ImageView) view.findViewById(R.id.image);
holder.chBox = (CheckBox) view.findViewById(R.id.checkBox);
view.setTag(holder);
} else{
holder = (ViewHolder) view.getTag();
}
holder.chBox.setChecked(datas.get(position).isChecked);
imageLoader.displayImage(datas.get(position).url, holder.image, options);
return view;
}

private static class ViewHolder {
ImageView image;
CheckBox chBox;
}
}


ListView的item布局文件layout/listview_item:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:paddingRight="8dp" >

<ImageView
android:id="@+id/image"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:layout_marginBottom="6dp"
android:layout_marginTop="6dp"
android:background="@drawable/ic_empty"
android:gravity="center_vertical"
android:scaleType="centerCrop" />

<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="@null"
android:button="@drawable/checkbox_selector"
android:focusable="false"
android:gravity="center_vertical" />

</RelativeLayout>


Demo下载地址
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐