我试图在其中显示包含所有用户图片的网格视图。
但是从Android MediaStore加载图像。
我必须做一个请求然后循环结果中的图像来获取每个缩略图,这会导致一个巨大的滞后!
如何将这些调用压缩为单个请求,或者如何在此节省更多时间?
String[] projection = new String[]{
MediaStore.Images.Media._ID,
MediaStore.Images.Media.TITLE,
MediaStore.Images.Media.DATA,
MediaStore.Images.Media.MIME_TYPE,
MediaStore.Images.Media.SIZE,
MediaStore.Images.Media.DATE_TAKEN,
MediaStore.Images.Media.DATE_ADDED,
MediaStore.Images.Media.DATE_MODIFIED,
MediaStore.Images.Media.ORIENTATION
};
String sortOrder = MediaStore.Images.Media.DATE_ADDED + " DESC, " +
MediaStore.Images.Media.DATE_MODIFIED + " DESC";
ContentResolver cr = ctx.getContentResolver();
final Cursor cursorImages = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection,
null,
null,
sortOrder);
ArrayList<ImageDataHolder> images = new ArrayList<ImageDataHolder>(cursorImages.getCount());
if (cursorImages.moveToFirst()) {
final int dataColumn = cursorImages.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
final int idColumn = cursorImages.getColumnIndexOrThrow(MediaStore.Images.Media._ID);
do {
final Long imageID = cursorImages.getLong(idColumn);
final String imagePath = cursorImages.getString(dataColumn);
final int imageOrientation = cursorImages.getInt(dataColumn);
// create new cursor to get thumbnail path
final Cursor cursorThumb = MediaStore.Images.Thumbnails.queryMiniThumbnail(ctx.getContentResolver(), imageID,
MediaStore.Images.Thumbnails.MINI_KIND, null);
String thumbPath = "";
if (cursorThumb != null) {
if (cursorThumb.getCount() > 0) {
cursorThumb.moveToFirst();
thumbPath = cursorThumb.getString(cursorThumb.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.DATA));
}
cursorThumb.close(); // cleanup crew, isle 7
}
// create new ImageDataHolder and add to images ArrayList
// only if we have a thumbnail to show
if (thumbPath != "")
images.add(new ImageDataHolder(imagePath, thumbPath, imageID, imageOrientation));
} while (cursorImages.moveToNext());
}
cursorImages.close();
return images;