Android media viewer glide video thumb
Introduction
The android media viewer glide video thumb tutorial series describes how to generate and display the video thumbnails along with the image thumbnails.
Get Code
The code can be found on github from the following instructions below
https://github.com/mobapptuts/media-thumbnail-viewer.git Tag
media-viewer-recyclerview
or you can run this command
git clone https://github.com/mobapptuts/media-thumbnail-viewer.git –branch
media-viewer-recyclerview
This video describes how to import the code from github using android studio and also how to use git tags
Steps
Add the Glide dependencies in the gradle build file
Note that it’s best to use the latest supported versions of these libraries.
dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.4.0' compile 'com.android.support:recyclerview-v7:23.4.0' compile 'com.github.bumptech.glide:glide:3.5.2' compile 'com.android.support:support-v4:23.0.0' }
Add the data field to the loader projection
This field contains to Uri to the media file
@Override public Loader<Cursor> onCreateLoader(int id, Bundle args) { String[] projection = { MediaStore.Files.FileColumns._ID, MediaStore.Files.FileColumns.DATE_ADDED, MediaStore.Files.FileColumns.DATA, MediaStore.Files.FileColumns.MEDIA_TYPE }; String selection = MediaStore.Files.FileColumns.MEDIA_TYPE + "=" + MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE + " OR " + MediaStore.Files.FileColumns.MEDIA_TYPE + "=" + MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO; return new CursorLoader( this, MediaStore.Files.getContentUri("external"), projection, selection, null, MediaStore.Files.FileColumns.DATE_ADDED + " DESC" ); }
Create a method to get the Uri of the media file
private Uri getUriFromMediaStore(int position) { int dataIndex = mMediaStoreCursor.getColumnIndex(MediaStore.Files.FileColumns.DATA); mMediaStoreCursor.moveToPosition(position); String data = mMediaStoreCursor.getString(dataIndex); return Uri.parse("file://" + data); }
In the onBindViewHolder method implement the Glide api with the Uri from the previous method
@Override public void onBindViewHolder(ViewHolder holder, int position) { Glide.with(mActivity) .load(getUriFromMediaStore(position)) .override(96, 96) .centerCrop() .into(holder.getImageView()); }
Android media viewer glide video thumb summary
The android Glide image loading library describes how to use Glide for loading the MediaStore images instead of the MediaStore thumbnail api.
This has a number of additional benefits including
- Simpler implementation
- Background thread support
- Caching support
- Improved performance