@Tyhj
2016-12-20T12:03:58.000000Z
字数 1422
阅读 1642
Android
原文:https://www.zybuluo.com/Tyhj/note/606822
参考:http://blog.csdn.net/qq_22770457/article/details/51492937
展示图库我也做过,用的是一个比较流行的图片加载工具,我的手机倒是没问题,但是在我同学那个垃圾魅蓝上还是OOM了,之前也没有管太多,今天又要用到,我就想了一下,之前我获取手机联系人头像的时候是可以获取缩略图的,那么图库应该也有吧,我网上查了一下果然有,那么再配合图片加载工具就简单了呀。获取方法如下:
/**
* 得到本地图片文件
* @param context
* @return
*/
public static ArrayList<HashMap<String,String>> getAllPictures(Context context) {
ArrayList<HashMap<String,String>> picturemaps = new ArrayList<>();
HashMap<String,String> picturemap;
ContentResolver cr = context.getContentResolver();
//先得到缩略图的URL和对应的图片id
Cursor cursor = cr.query(
Thumbnails.EXTERNAL_CONTENT_URI,
new String[]{
Thumbnails.IMAGE_ID,
Thumbnails.DATA
},
null,
null,
null);
if (cursor.moveToFirst()) {
do {
picturemap = new HashMap<>();
picturemap.put("image_id_path",cursor.getInt(0)+"");
picturemap.put("thumbnail_path",cursor.getString(1));
picturemaps.add(picturemap);
} while (cursor.moveToNext());
cursor.close();
}
//再得到正常图片的path
for (int i = 0;i<picturemaps.size();i++) {
picturemap = picturemaps.get(i);
String media_id = picturemap.get("image_id_path");
cursor = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
new String[]{
MediaStore.Images.Media.DATA
},
MediaStore.Audio.Media._ID+"="+media_id,
null,
null
);
if (cursor.moveToFirst()) {
do {
picturemap.put("image_id_path",cursor.getString(0));
picturemaps.set(i,picturemap);
} while (cursor.moveToNext());
cursor.close();
}
}
return picturemaps;
}
其实在参考文章中楼主写错了的,仔细对照我的
thumbnail_path为缩略图
image_id_path为原图