我正在尝试为 android 设计一个自定义画廊。我可以显示图像,但视频出了点问题。我是Android新手,任何帮助将不胜感激:)
问题是我有两个数据源,一个用于图像,另一个用于视频,那么如何将数据合并并显示在一起。
代码:
public class MainActivity extends Activity {
private int count;
private int vcount;
private Bitmap[] thumbnails;
private String[] arrPath;
private Bitmap[] vthumbnails;
private String[] varrPath;
private ImageAdapter imageAdapter;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//sets UI of main page from activity_main.xml
setContentView(R.layout.activity_main);
//get images from SD card and sort
final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID };
final String orderBy = MediaStore.Images.Media.DATE_TAKEN;
Cursor imagecursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null,null, orderBy + " DESC");
//this.imageUrls = new ArrayList<String>();
int image_column_index = imagecursor.getColumnIndex(MediaStore.Images.Media._ID);
this.count = imagecursor.getCount();
this.thumbnails = new Bitmap[this.count];
this.arrPath = new String[this.count];
for (int i = 0; i < this.count; i++) {
imagecursor.moveToPosition(i);
int id = imagecursor.getInt(image_column_index);
int dataColumnIndex = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA);
thumbnails[i] = MediaStore.Images.Thumbnails.getThumbnail(
getApplicationContext().getContentResolver(), id,
MediaStore.Images.Thumbnails.MICRO_KIND, null);
arrPath[i]= imagecursor.getString(dataColumnIndex);
}
final String[] parameters = { MediaStore.Video.Media._ID,
MediaStore.Video.Media.DATA,
MediaStore.Video.Media.DISPLAY_NAME,
MediaStore.Video.Media.SIZE, MediaStore.Video.Media.DURATION,
MediaStore.Video.Media.DATE_TAKEN,MediaStore.Video.Thumbnails.DATA};
//final String vOrderBy = MediaStore.Video.Media.DATE_TAKEN;
Cursor videocursor = getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,parameters, null, null, null);
int video_column_index = videocursor.getColumnIndex(MediaStore.Video.Media._ID);
this.vcount = videocursor.getCount();
this.vthumbnails = new Bitmap[this.vcount];
this.varrPath = new String[this.vcount];
for (int j = 0; j < this.vcount; j++) {
videocursor.moveToPosition(j);
int id = videocursor.getInt(video_column_index);
int vDataColumnIndex = videocursor.getColumnIndex(MediaStore.Video.Media.DATA);
vthumbnails[j] = MediaStore.Video.Thumbnails.getThumbnail(
getApplicationContext().getContentResolver(), id,
MediaStore.Video.Thumbnails.MICRO_KIND, null);
varrPath[j]= videocursor.getString(vDataColumnIndex);
}
GridView imagegrid = (GridView) findViewById(R.id.PhoneImageGrid);
imageAdapter = new ImageAdapter();
imagegrid.setAdapter(imageAdapter);
//imagecursor.close();
}
public class ImageAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public ImageAdapter() {
mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return count;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.galleryitem, null);
holder.imageview = (ImageView) convertView.findViewById(R.id.thumbImage);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
holder.imageview.setId(position);
holder.imageview.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//TODO Auto-generated method stub
int id = v.getId();
Intent intent = new Intent(MainActivity.this,Lastscreen.class);
intent.setDataAndType(Uri.parse("file://" + arrPath[id]), "image/*");
intent.putExtra("path", arrPath[id]);
startActivity(intent);
}
});
holder.imageview.setImageBitmap(thumbnails[position]);
return convertView;
}
}
class ViewHolder {
ImageView imageview;
}
}