我现在有一个类似 youtube 缩略图样式的项目,我已经创建了自定义 simplecursorapader,它使用 mediastore 显示来自 sdcard 的视频的图像、标题、艺术家、持续时间,
我的问题是唯一的图像是错误的,但标题,艺术家,持续时间已经正常,我注意到图像没有从视频中获得正确的缩略图,标题,艺术家,持续时间是唯一没有问题的,
我的 sd 卡上有 10 个视频,列表视图以正确的顺序显示标题、艺术家、持续时间,但图像不正确,并且 ii 拖动列表图像也发生变化,我也想获得正确的图像每个列表顺序的视频..
这是我的代码
public class TABThisWeek extends ListActivity {
Cursor videoCursor;
int videoColumnIndex;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Uri sourceUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
String[] projection = { MediaStore.Video.Media._ID,
MediaStore.Video.Media.DATA, MediaStore.Video.Media.TITLE,
MediaStore.Video.Media.DISPLAY_NAME,
MediaStore.Video.Media.ARTIST, MediaStore.Video.Media.DURATION };
String orderBy = MediaStore.Video.Media.TITLE;
// CREATE CURSOR THAT WILL HOLD ALL VALUE
videoCursor = getContentResolver().query(sourceUri, projection, null,
null, orderBy);
// THE DESIRED COLUMNS TO BE BOUND
String[] from = { MediaStore.Video.Media.TITLE,
MediaStore.Video.Media.ARTIST, MediaStore.Video.Media.DURATION};
// THE XML DEFINED VIEWS WHICH THE DATA WILL BE BOUND TO
int[] to = { R.id.list_Title, R.id.list_Artist, R.id.list_Duration };
/*
* CREATE THE ADAPTER USING THE CURSOR POINTING TO THE DESIRED DATA AS
* WELL AS THE LAYOUT INFORMATION
*/
MyCustomAdapter adapter = new MyCustomAdapter(this,
R.layout.list_row_items, videoCursor, from, to);
setListAdapter(adapter);
setListViewAttributes(); // set listview divider color etc
}
/* get ListActivity's own ListView and sets the divider color */
private void setListViewAttributes() {
ListView lvTab = getListView();
ColorDrawable cd = new ColorDrawable(this.getResources().getColor(
R.color.color_divider_black));
lvTab.setDivider(cd);
lvTab.isScrollbarFadingEnabled();
lvTab.setVerticalFadingEdgeEnabled(true);
lvTab.setFadingEdgeLength(25);
lvTab.setDividerHeight(1);
}
/* Custom Adapter for TabThisWeek */
public class MyCustomAdapter extends SimpleCursorAdapter {
private Cursor c;
private int layout;
private final LayoutInflater inflater;
private MyViewHolder holder;
BitmapFactory.Options options;
public MyCustomAdapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
// this.context = context;
this.c = c;
this.layout = layout;
this.inflater = LayoutInflater.from(context);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return inflater.inflate(layout, parent, false);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
super.bindView(view, context, cursor);
holder = (MyViewHolder) view.getTag();
if (holder == null) {
holder = new MyViewHolder();
holder.titleHolder = (TextView) view
.findViewById(R.id.list_Title);
holder.artistHolder = (TextView) view
.findViewById(R.id.list_Artist);
holder.durationHolder = (TextView) view
.findViewById(R.id.list_Duration);
holder.imageHolder = (ImageView) view
.findViewById(R.id.list_Image);
/* getting the index because auto loop */
holder.titleIndex = cursor
.getColumnIndexOrThrow(MediaStore.Video.Media.TITLE);
holder.artistIndex = cursor
.getColumnIndexOrThrow(MediaStore.Video.Media.ARTIST);
holder.durationIndex = cursor
.getColumnIndexOrThrow(MediaStore.Video.Media.DURATION);
holder.imageIndex = cursor.getInt(cursor
.getColumnIndexOrThrow(MediaStore.Video.Media._ID));
view.setTag(holder);
}
/* set the Title but if null set to default from resources */
try {
holder.titleHolder.setText(cursor.getString(holder.titleIndex));
} catch (Exception e) {
holder.titleHolder.setText(getResources().getString(
R.string.default_text_title));
}
/* set the artist but if null set to default from resources */
try {
holder.artistHolder.setText(cursor
.getString(holder.artistIndex)); // temp
} catch (Exception e) {
holder.artistHolder.setText(getResources().getString(
R.string.default_text_artist));
}
/* set the time duration if null set to default */
try {
holder.durationHolder.setText(cursor
.getString(holder.durationIndex)); // temp
} catch (Exception e) {
holder.durationHolder.setText(getResources().getString(
R.string.default_text_duration));
}
try {
options = new BitmapFactory.Options();
options.inDither = false;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
options.inPreferredConfig = Bitmap.Config.RGB_565;
holder.bitmapVidThumb = MediaStore.Video.Thumbnails
.getThumbnail(context.getContentResolver(),
holder.imageIndex,
MediaStore.Video.Thumbnails.MICRO_KIND, options);
holder.imageHolder.setImageBitmap(holder.bitmapVidThumb);
} catch (Exception e) {
holder.imageHolder.setBackgroundDrawable(getResources()
.getDrawable(R.drawable.default_img));
}
/* set default value if title is null */
if (holder.titleHolder.getText().toString().equals("")) {
holder.titleHolder.setText(getResources().getString(
R.string.default_text_title));
}
/* set default value if artist is null */
if (holder.artistHolder.getText().toString().equals("")) {
holder.artistHolder.setText(getResources().getString(
R.string.default_text_artist));
}
/* to get and check if time is below 0 for exception */
holder.durationTemp = Long.parseLong(holder.durationHolder
.getText().toString());
if (holder.durationTemp <= 0) {
holder.durationHolder.setText(getResources().getString(
R.string.default_text_duration));
}
}
/* my nested view holder class */
class MyViewHolder {
Bitmap bitmapVidThumb;
ImageView imageHolder;
TextView titleHolder;
TextView artistHolder;
TextView durationHolder;
int imageIndex;
int titleIndex;
int artistIndex;
int durationIndex;
long durationTemp;
}
}
}