hotmeatballsoup
hotmeatballsoup

Reputation: 595

Querying Android ContentResolver for gallery files and determining image vs video

I'm trying to make a simple Android app for my own phone that looks at my Gallery, and for each image/video file:

Essentially, I'll have the following data model:

public class MediaFileModel {

  private Bitmap thumbnail;
  private String absPath;

  // ctor, getters and setters omitted for brevity
  
}

And I need something that looks at all the files in my Gallery and yields a List<MediaFileModel>. My best attempt is here:

public List<MediaFileModel> getAllGalleryMedia() {

  String[] projection = { MediaStore.MediaColumns.DATA };
  List<MediaFileModel> galleryMedia = new ArrayList<>();
  Cursor cursor = getActivity().getContentResolver().query(
    MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
    projection,
    null,
    null,
    null);

  // I believe here I'm iterating over all the gallery files (???)
  while(cursor.moveToNext()) {
    MediaFileModel next = new MediaFileModel();

    // is this how I get the abs path of the current file?!?
    String absPath = cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.DATA));

    Bitmap thumbnail = null;
    if (true /* ??? cursor is pointing to a media file that is an image/photo ??? */) {
      thumbnail = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(absPath), 64, 64);
    } else {
      // else we have a video (I assume???)
      thumbnail = ThumbnailUtils.createVideoThumbnail(absPath, MediaStore.Images.Thumbnails.MINI_KIND);
    }

    next.setThumbnail(thumbnail);
    next.setAbsPath(absPath);

    galleryMedia.add(next);
    
  }

  return galleryMedia;
  
}

However I'm not sure if my media query is setup correctly and I'm definitely not sure how to determine whether the file is an image/photo of a video, which I (believe I) need so that I can use the correct method for obtaining the thumbnail Bitmap.

Can anyone help nudge me over the finish line here?

Upvotes: 3

Views: 1530

Answers (1)

user16930239
user16930239

Reputation: 9717

Here is a code I was using for something similar, I hope this will help:

//The code has been removed

Note: Sorry I've removed the code because I am not sure if it has some portion copied from other open-sourced codes, I am using it in my apps but I can't remember if it is quoted from other source, so I've removed it, also it could be auto-completed using github copilot

Upvotes: 2

Related Questions