title: Get Images and Videos from Android Phone into Custom Gallery
body: <p>I'm trying to create a custom gallery that allows users to pick from all the photos and videos contained on their Android device. I know how to create a gallery of just photos and just videos, but if I want to combine both, how can I do this?  </p>
  
  <p>I think the issue comes down to how I create my cursor.  To select all videos, I created the cursor this way:</p>
  
  <pre><code>String[] videoParams = {MediaStore.Video.Media._ID,
          MediaStore.Video.Media.DATA,
          MediaStore.Video.Media.DATE_TAKEN,
          MediaStore.Video.Thumbnails.DATA};
  
  videocursor = getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, videoParams, null, null, null);
  </code></pre>
  
  <p><strong>If I want to query all the media files, not just video, what do I do?</strong></p>
  
  <p>This is what I tried, based off of: <a href="https://stackoverflow.com/questions/18457404/custom-gallery-with-images-and-videos-in-android-to-select-multiple-items">Custom Gallery with Images and Videos in android to select multiple items</a></p>
  
  <pre><code>    String selectionMimeType = MediaStore.Files.FileColumns.MIME_TYPE + "=?";
      String jpg_mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension("jpg");
      String png_mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension("png");
      String mp4_mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension("mp4");
      String[] selectionArgs = new String[]{jpg_mimeType, png_mimeType, mp4_mimeType};
  
      mediaCursor = getContentResolver().query(MediaStore.Files.getContentUri("internal"), null, selectionMimeType, selectionArgs, MediaStore.Files.FileColumns.DATE_ADDED);
  </code></pre>
  
  <p>This gives me the error <strong>java.lang.IllegalArgumentException: Cannot bind argument at index 3 because the index is out of range.  The statement has 1 parameters at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:167)</strong></p>
  
  <p>Perhaps my approach is completely wrong, but I can't find any examples of custom android galleries using both images and videos, which is bizarre to me as this seems like it would be a common thing to create.</p>
  
  <p>Here's all of my code, in case it's helpful:</p>
  
  <pre><code>public class GridViewCompatActivity extends Activity {
  
      GridViewCompat gridView;
  
      private static final String TAG = "GridViewCompatActivity"; 
  
      Cursor videocursor;
      Cursor mediaCursor;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_grid_view_compat);
  
          gridView = (GridViewCompat) findViewById(R.id.gridView1);
  
          // NOTE: We are using setChoiceMode, as I said, its a drop-in replacement
          gridView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
          gridView.setAdapter(new ImageAdapter(getApplicationContext()));
  
          gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  
              @Override
              public void onItemClick(AdapterView&lt;?&gt; view, View arg1, int pos, long id) {
                  // We need to invalidate all views on 4.x versions
                  GridViewCompat gridView = (GridViewCompat) view;
                  gridView.invalidateViews();
              }
  
          });
  
          findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {
  
              @Override
              public void onClick(View v) {
                  // TODO Auto-generated method stub
                  SparseBooleanArray checkArray;
                  checkArray = gridView.getCheckedItemPositions();
  
                  String selectedPos = "Selected positions: ";
                  int count = checkArray.size();
                  for (int i = 0; i &lt; count; i++) {
                      if (checkArray.valueAt(i))
                          selectedPos += checkArray.keyAt(i) + ",";
                  }
  
                  Intent intent = new Intent();
                  intent.putExtra("result", selectedPos);
                  setResult(Activity.RESULT_OK, intent);
                  finish();
              }
          });
      }
  
      public class ImageAdapter extends BaseAdapter {
          private Context mContext;
  
          public ImageAdapter(Context c) {
              mContext = c;
          }
  
          public int getCount() {
              Log.d(TAG, "number of media: " + Integer.toString(MediaStore.Files.FileColumns.DATA.length()));
              int mediaParams = MediaStore.MediaColumns.DATA.length();
              return mediaParams;
          }
  
          public Object getItem(int position) {
              return null;
          }
  
          public long getItemId(int position) {
              return 0;
          }
  
          // create a new grid view item for each item referenced by the Adapter
          @TargetApi(Build.VERSION_CODES.HONEYCOMB)
          public View getView(int position, View convertView, ViewGroup parent) {
              ImageView imageView;
              CheckBox checkBox;
              if (convertView == null) {
                  LayoutInflater layoutInflater = LayoutInflater.from(mContext);
                  convertView = layoutInflater.inflate(R.layout.grid_view_item, parent, false);
              }
              imageView = (ImageView) convertView.findViewById(R.id.imageView1);
              checkBox = (CheckBox) convertView.findViewById(R.id.checkBox1);
  
              GridViewCompat gvc = (GridViewCompat) parent;
              if (gvc.getChoiceMode() == ListView.CHOICE_MODE_MULTIPLE) {
                  SparseBooleanArray checkArray;
                  checkArray = gvc.getCheckedItemPositions();
  
                  checkBox.setChecked(false);
                  if (checkArray != null) {
                      if (checkArray.get(position)) {
                          checkBox.setChecked(true);
                      }
                  }
  
              }
              // imageView.setImageResource(mThumbIds[position]);
              Bitmap bmThumbnail;
              Log.d(TAG, "position: " + position);
              mediaCursor = getContentResolver().query(MediaStore.Files.getContentUri("internal"), null, selectionMimeType, selectionArgs, MediaStore.Files.FileColumns.DATE_ADDED);
              Log.d(TAG, Integer.toString(mediaCursor.getCount()));
  
              for (int i = 0; i &lt; mediaCursor.getCount(); i++){
                  mediaCursor.moveToPosition(i);
                  Boolean isVideo = mediaCursor.getString(mediaCursor.getColumnIndex(MediaStore.Video.Thumbnails.DATA)).length() &gt; 0;
                  Log.d(TAG, "isVideo: " + isVideo);
                  String mediaPath = ""; 
                  if(isVideo){
                      mediaPath = mediaCursor.getString(mediaCursor.getColumnIndex(MediaStore.Video.Thumbnails.DATA));
                      video_paths.add(mediaPath);    
                  }else{
                      mediaPath = mediaCursor.getString(mediaCursor.getColumnIndex(MediaStore.Images.Media.DATA));
                      video_paths.add(mediaPath);
                  }
                  Log.d(TAG, "mediaPath: " +mediaPath);
              }     
  
              mediaCursor.moveToPosition(position);
              String video_path = mediaCursor.getString(mediaCursor.getColumnIndex(MediaStore.Video.Thumbnails.DATA));
              Log.d(TAG, "video_path: " + video_path);
              imageView.setImageBitmap(ThumbnailUtils.createVideoThumbnail(video_path, Thumbnails.MICRO_KIND));
  
              return convertView;
          }
  
          ArrayList&lt;String&gt; video_paths = new ArrayList&lt;String&gt;();
  
          String selectionMimeType = MediaStore.Files.FileColumns.MIME_TYPE + "=?";
          String jpg_mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension("jpg");
          String png_mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension("png");
          String mp4_mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension("mp4");
          String[] selectionArgs = new String[]{jpg_mimeType, png_mimeType, mp4_mimeType};
  
  //        String[] videoParams = {MediaStore.Video.Media._ID,
  //                MediaStore.Video.Media.DATA,
  //                MediaStore.Video.Media.DATE_TAKEN,
  //                MediaStore.Video.Thumbnails.DATA};
  //        }
  }
  }
  </code></pre>