Search code examples
androidandroid-recyclerviewandroid-gallery

How to differentiate between videos and images when getting from gallery into a recyclerview?


I want to get images and videos from phone gallery and display in a recyclerveiw. In recyclerview adapter, I've two viewtypes, one for displaying images and second for displaying videos. But I don't know how to set a check between videos and images to differentiate between two viewtypes.

This how I'm getting images from gallery, and its working fine. But what if I also get videos just like this and want to set those in other viewtype, just say videoview.

//method to get the list of images from gallery
private ArrayList<String> getAllShownImagesPath(Activity activity) {
    Uri uri;
    Cursor cursor;
    int column_index_data, column_index_folder_name;
    ArrayList<String> listOfAllImages = new ArrayList<String>();
    String absolutePathOfImage = null;
    uri = android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;

    String[] projection = {MediaStore.MediaColumns.DATA,
            MediaStore.Images.Media.BUCKET_DISPLAY_NAME};


    cursor = activity.getContentResolver().query(uri, projection, null,
            null, null);

    column_index_data = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
    column_index_folder_name = cursor
            .getColumnIndexOrThrow(MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
    while (cursor.moveToNext()) {
        absolutePathOfImage = cursor.getString(column_index_data);

        listOfAllImages.add(absolutePathOfImage);
    }
    return listOfAllImages;
}

Please guide me how to get all videos and images from gallery with a single cursor or method and display those in different viewtypes in recyclerview.


Solution

  • You can get the MIME Type of a media retrieved from Gallery in your recyclerview. https://developer.android.com/reference/android/webkit/MimeTypeMap , refer this official documentation. This is the clear solution as dealing with media files. Give it a try.