Android-从两个不同的源提供gridview的输入

时间:2013-09-18 10:08:38

标签: android gridview android-gridview android-adapter baseadapter

我正在尝试为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;
}

}

1 个答案:

答案 0 :(得分:0)

  1. 您必须在网格视图中仅显示图像。
  2. 相应的视频应存储在SD卡中
  3. 图像文件名和视频文件名
  4. 首先获取arraylist中的图像名称并获取视频名称的另一个arraylist,然后比较它并获取数据。
  5. 由于