我在同一个文件夹中有大约150个图像和150个视频,我需要用缩略图填充列表视图中的所有文件。一切正常,但速度太慢,因为它多次创建VideoThumbnail。我尝试了很多东西但是我无法使listview顺利进行。请看看我的代码。提前谢谢。
public class FilesAdapter extends ArrayAdapter<String> {
private List<String> mpath;
private Context mContext;
public FilesAdapter(Context context, List<String> path) {
super(context, R.layout.fileadapter_list);
this.mContext = context;
this.mpath = path;
}
class Helper {
public TextView txtTitle;
public ImageView imageView;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.fileadapter_list, null);
Helper h = new Helper();
h.txtTitle = (TextView) convertView.findViewById(R.id.txt);
h.imageView = (ImageView) convertView.findViewById(R.id.img);
convertView.setTag(h);
}
Helper myHelper = (Helper) convertView.getTag();
File file = new File(mpath.get(position));
myHelper.txtTitle.setText(file.getName());
if (file.isDirectory()) {
Picasso.with(mContext).load(R.drawable.folder)
.placeholder(R.drawable.folder).error(R.drawable.folder)
.into(myHelper.imageView);
} else {
String sitem = mpath.get(position);
String item_ext = null;
try {
item_ext = sitem.substring(sitem.lastIndexOf("."),
sitem.length());
} catch (IndexOutOfBoundsException e) {
item_ext = "";
}
if (item_ext.equalsIgnoreCase(".jpeg")
|| item_ext.equalsIgnoreCase(".jpg")
|| item_ext.equalsIgnoreCase(".png")
|| item_ext.equalsIgnoreCase(".gif")
|| item_ext.equalsIgnoreCase(".tiff")) {
myHelper.imageView.clearColorFilter();
Picasso.with(mContext).load(Uri.fromFile(file))
.placeholder(R.drawable.holder).error(R.drawable.error)
.centerInside().resize(100, 100)
.into(myHelper.imageView);
} else if (item_ext.equalsIgnoreCase(".m4v")
|| item_ext.equalsIgnoreCase(".3gp")
|| item_ext.equalsIgnoreCase(".wmv")
|| item_ext.equalsIgnoreCase(".mp4")
|| item_ext.equalsIgnoreCase(".ogg")
|| item_ext.equalsIgnoreCase(".wav")) {
Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(
file.getAbsolutePath(),
MediaStore.Video.Thumbnails.MICRO_KIND);
Picasso.with(mContext).load(getImageUri(mContext, bitmap))
.centerInside().resize(100, 100)
.placeholder(R.drawable.holder).error(R.drawable.error)
.into(myHelper.imageView);
}
}
return convertView;
}
public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 20, bytes);
String path = Images.Media.insertImage(inContext.getContentResolver(),
inImage, "Title", null);
return Uri.parse(path);
}
}