我在recyclerview中实现了gridview,以缩略图显示图像和视频。
我使用以下代码来获取 onBindView
方法
使用以下代码获取图片缩略图:
bitmap = MediaStore.Images.Thumbnails.getThumbnail(context
.getApplicationContext().getContentResolver(), item.getImgId(),
MediaStore.Images.Thumbnails.MICRO_KIND, null);
使用以下代码获取视频缩略图:
bitmap = MediaStore.Video.Thumbnails.getThumbnail(context
.getApplicationContext().getContentResolver(), item.getImgId(),
MediaStore.Images.Thumbnails.MICRO_KIND, null);
但每次我滚动视图棒(向下或向上滚动需要一些时间)。我认为这是因为我在 onBindView
方法中加载缩略图。
帮助我改进此代码,以便可以顺利滚动。 或任何API轻松完成。
答案 0 :(得分:0)
将以下代码放入onCreate:
imageAdapter = new ImageAdapter();
imageAdapter.initialize();
以下是用于从本地存储中获取图像和视频的适配器:
public class ImageAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public ArrayList<ImageItem> images = new ArrayList<ImageItem>();
public ImageAdapter() {
mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void initialize() {
images.clear();
final String[] columns = {MediaStore.Images.Thumbnails._ID};
final String orderBy = MediaStore.Images.Media._ID;
Cursor imagecursor = managedQuery(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns,
null, null, orderBy);
int int_position = 0;
Uri uri;
Cursor cursor;
int column_index_data, column_index_folder_name, column_id, thum;
String absolutePathOfImage = null;
uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
String[] projection = {MediaStore.MediaColumns.DATA, MediaStore.Video.Media.BUCKET_DISPLAY_NAME, MediaStore.Video.Media._ID, MediaStore.Video.Thumbnails.DATA};
cursor = getApplicationContext().getContentResolver().query(uri, projection, null, null, orderBy + " DESC");
column_index_data = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
column_index_folder_name = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.BUCKET_DISPLAY_NAME);
column_id = cursor.getColumnIndexOrThrow(MediaStore.Video.Media._ID);
thum = cursor.getColumnIndexOrThrow(MediaStore.Video.Thumbnails.DATA);
while (cursor.moveToNext()) {
int id = cursor.getColumnIndex(MediaStore.Video.Media._ID);
absolutePathOfImage = cursor.getString(column_index_data);
Log.e("Column", absolutePathOfImage);
Log.e("Folder", cursor.getString(column_index_folder_name));
Log.e("column_id", cursor.getString(column_id));
Log.e("thum", cursor.getString(thum));
ImageItem obj_model = new ImageItem();
obj_model.id = id;
obj_model.thumb = cursor.getString(thum);
images.add(obj_model);
}
if (imagecursor != null) {
int image_column_index = imagecursor
.getColumnIndex(MediaStore.Images.Media._ID);
int count = imagecursor.getCount();
for (int i = 0; i < count; i++) {
imagecursor.moveToPosition(i);
int id = imagecursor.getInt(image_column_index);
ImageItem imageItem = new ImageItem();
imageItem.id = id;
lastId = id;
imageItem.img = MediaStore.Images.Thumbnails.getThumbnail(
getApplicationContext().getContentResolver(), id,
MediaStore.Images.Thumbnails.MICRO_KIND, null);
images.add(imageItem);
}
imagecursor.close();
}
notifyDataSetChanged();
}
public void checkForNewImages() {
//Here we'll only check for newer images
final String[] columns = {MediaStore.Images.Thumbnails._ID};
final String orderBy = MediaStore.Images.Media._ID;
Cursor imagecursor = managedQuery(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns,
MediaStore.Images.Media._ID + " > " + lastId, null, orderBy);
int image_column_index = imagecursor
.getColumnIndex(MediaStore.Images.Media._ID);
int count = imagecursor.getCount();
for (int i = 0; i < count; i++) {
imagecursor.moveToPosition(i);
int id = imagecursor.getInt(image_column_index);
ImageItem imageItem = new ImageItem();
imageItem.id = id;
lastId = id;
imageItem.img = MediaStore.Images.Thumbnails.getThumbnail(
getApplicationContext().getContentResolver(), id,
MediaStore.Images.Thumbnails.MICRO_KIND, null);
imageItem.selection = true; //newly added item will be selected by default
images.add(imageItem);
}
imagecursor.close();
notifyDataSetChanged();
}
public int getCount() {
return images.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, final ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.galleryitem1, null);
holder.imageview = (ImageView) convertView
.findViewById(R.id.thumbImage);
holder.ivVideoIcon = (ImageView) convertView
.findViewById(R.id.ivVideoIcon);
holder.checkbox = (CheckBox) convertView
.findViewById(R.id.itemCheckBox);
holder.pbImageLoad = (ProgressBar) convertView
.findViewById(R.id.pbImageLoad);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
ImageItem item = images.get(position);
holder.checkbox.setId(position);
holder.imageview.setId(position);
if (item.thumb != null) {
/*Picasso.with(AndroidCustomGalleryActivity.this)
.load("file" + item.thumb)
.into(holder.imageview);*/
holder.ivVideoIcon.setVisibility(View.VISIBLE);
Glide.with(AndroidCustomGalleryActivity.this).load(Uri.fromFile(new File(item.thumb)))
.listener(new RequestListener<Uri, GlideDrawable>() {
@Override
public boolean onException(Exception e, Uri model, Target<GlideDrawable> target, boolean isFirstResource) {
holder.pbImageLoad.setVisibility(View.GONE);
return false;
}
@Override
public boolean onResourceReady(GlideDrawable resource, Uri model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
holder.pbImageLoad.setVisibility(View.GONE);
return false;
}
})
.skipMemoryCache(false)
.into(holder.imageview);
} else {
// holder.imageview.setImageBitmap(item.img);
if (item.img != null) {
holder.ivVideoIcon.setVisibility(View.GONE);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
item.img.compress(Bitmap.CompressFormat.PNG, 100, stream);
Glide.with(AndroidCustomGalleryActivity.this).load(stream.toByteArray()).asBitmap().listener(new RequestListener<byte[], Bitmap>() {
@Override
public boolean onException(Exception e, byte[] model, Target<Bitmap> target, boolean isFirstResource) {
holder.pbImageLoad.setVisibility(View.GONE);
return false;
}
@Override
public boolean onResourceReady(Bitmap resource, byte[] model, Target<Bitmap> target, boolean isFromMemoryCache, boolean isFirstResource) {
holder.pbImageLoad.setVisibility(View.GONE);
return false;
}
}).skipMemoryCache(false).into(holder.imageview);
}
}
holder.checkbox.setChecked(item.selection);
return convertView;
}
}
class ViewHolder {
ImageView imageview;
ImageView ivVideoIcon;
CheckBox checkbox;
ProgressBar pbImageLoad;
}
class ImageItem {
boolean selection;
int id;
Bitmap img;
String thumb;
}