我有一个应用程序 - 简单的文件管理器,它使用 MediaStore.Video.Thumbnails.getThumbnail() 和 MediaStore.Images.Thumbnails.getThumbnail() 显示图片和视频的缩略图。但它在有很多图片的目录中运行缓慢。如何解决?
部分代码:
private ArrayList<File> filesList = new ArrayList<File>();
// ...
class FilesListAdapter extends BaseAdapter {
// ...
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
File file = filesList.get(position);
Bitmap thumbnailBitmap = null;
ContentResolver crThumb = getContentResolver();
convertView = LayoutInflater.from(mContext).inflate(mResource, parent, false);
ImageView thumbnail = (ImageView) convertView.findViewById(R.id.thumbnail);
if (file.isDirectory()) {
thumbnail.setImageResource(R.drawable.efp__ic_folder);
} else {
if (Build.VERSION.SDK_INT >= 5) {
try {
if (Arrays.asList(videoExtensions).contains(getFileExtension(file.getName()))) {
Cursor cursor = crThumb.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, new String[] { MediaStore.Video.Media._ID }, MediaStore.Video.Media.DATA + "='" + file.getAbsolutePath() + "'", null, null);
if (cursor != null) {
if (cursor.getCount() > 0) {
cursor.moveToFirst();
thumbnailBitmap = MediaStore.Video.Thumbnails.getThumbnail(crThumb, cursor.getInt(0), MediaStore.Video.Thumbnails.MICRO_KIND, null);
}
cursor.close();
}
} else if (Arrays.asList(imagesExtensions).contains(getFileExtension(file.getName()))) {
Cursor cursor = crThumb.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[] { MediaStore.Images.Media._ID }, MediaStore.Images.Media.DATA + "='" + file.getAbsolutePath() + "'", null, null);
if (cursor != null) {
if (cursor.getCount() > 0) {
cursor.moveToFirst();
thumbnailBitmap = MediaStore.Images.Thumbnails.getThumbnail(crThumb, cursor.getInt(0), MediaStore.Images.Thumbnails.MINI_KIND, null);
}
cursor.close();
}
}
} catch(Exception e) {
e.printStackTrace();
} catch (Error e) {
e.printStackTrace();
}
}
if (thumbnailBitmap == null) thumbnail.setImageResource(R.drawable.efp__ic_file);
else thumbnail.setImageBitmap(thumbnailBitmap);
}
// ...
return convertView;
}
}