Android media viewer adapter cursor

Introduction

The android media viewer adapter cursor tutorial describes how to add cursor support in the RecyclerView adapter.

Get Code

The code can be found on github from the following instructions below

https://github.com/mobapptuts/media-thumbnail-viewer.git Tag

media-store-adapter-cursor

or you can run this command

git clone https://github.com/mobapptuts/media-thumbnail-viewer.git –branch

media-store-adapter-cursor

This video describes how to import the code from github using android studio and also how to use git tags

Steps

Add a member for the cursor and the calling activity

private Cursor mMediaStoreCursor;
private final Activity mActivity;

 Initialise the activity in the constructor

public MediaStoreAdapter(Activity activity) {
       this.mActivity = activity;
   }

 Add a swap cursor method to load the cursor member and inform the adapter that the data has changed

private Cursor swapCursor(Cursor cursor) {
        if (mMediaStoreCursor == cursor) {
            return null;
        }
        Cursor oldCursor = mMediaStoreCursor;
        this.mMediaStoreCursor = cursor;
        if (cursor != null) {
            this.notifyDataSetChanged();
        }
        return oldCursor;
    }

 Provide a change cursor method for the loader

public void changeCursor(Cursor cursor) {
        Cursor oldCursor = swapCursor(cursor);
        if(oldCursor != null) {
            oldCursor.close();
        }
    }

 Change the  getItemCount method to support the cursor

@Override
   public int getItemCount() {
       return (mMediaStoreCursor == null) ? 0 : mMediaStoreCursor.getCount();
   }

Create a method to get the bitmap from the MediaStore

private Bitmap getBitmapFromMediaStore(int position) {
      int idIndex = mMediaStoreCursor.getColumnIndex(MediaStore.Files.FileColumns._ID);
      int mediaTypeIndex = mMediaStoreCursor.getColumnIndex(MediaStore.Files.FileColumns.MEDIA_TYPE);

      mMediaStoreCursor.moveToPosition(position);
      switch (mMediaStoreCursor.getInt(mediaTypeIndex)) {
          case MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE:
              return MediaStore.Images.Thumbnails.getThumbnail(
                      mActivity.getContentResolver(),
                      mMediaStoreCursor.getLong(idIndex),
                      MediaStore.Images.Thumbnails.MICRO_KIND,
                      null
              );
          case MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO:
              return MediaStore.Video.Thumbnails.getThumbnail(
                  mActivity.getContentResolver(),
                      mMediaStoreCursor.getLong(idIndex),
                      MediaStore.Video.Thumbnails.MICRO_KIND,
                      null
              );
          default:
              return null;
      }
  }

 

 Provide the bitmap to the ViewHolder in the onBindViewHolder method

@Override
   public void onBindViewHolder(ViewHolder holder, int position) {
       Bitmap bitmap = getBitmapFromMediaStore(position);
       if(bitmap != null) {
           holder.getImageView().setImageBitmap(bitmap);
       }
   }

 

Android media viewer adapter cursor summary

This android tutorial describes the steps involved for processing the information of the provided MediaStore cursor. Including getting the Video & Image id’s and getting the thumbnails from them.

And completion of the getItemCount & onBindViewHolder methods.

About The Author
-

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>