Skip to content

Instantly share code, notes, and snippets.

@tolmachevroman
Last active August 29, 2015 14:15
Show Gist options
  • Save tolmachevroman/5b2862f8ff813165a062 to your computer and use it in GitHub Desktop.
Save tolmachevroman/5b2862f8ff813165a062 to your computer and use it in GitHub Desktop.
AsynTask loader for Video Thumbnails
///
///Your adapter code
///
public View getView(int position, View convertView, ViewGroup parent) {
//example of usage
//let holder.image be some ImageView, mContentUri some video file Uri
new VideoThumbnailLoader(getContext(), holder.image, MediaStore.Images.Thumbnails.MICRO_KIND).execute(mContentUri);
}
///
package com.dev.kedzoh.utils;
import android.content.Context;
import android.graphics.Bitmap;
import android.media.ThumbnailUtils;
import android.net.Uri;
import android.os.AsyncTask;
import android.widget.ImageView;
import java.util.HashMap;
import java.util.Map;
/**
* Created by romantolmachev on 10/2/15.
*/
public class VideoThumbnailLoader extends AsyncTask<Uri, Void, Bitmap> {
private Context mContext;
private ImageView mImageView;
private int mThumbnailType;
private String mPath;
private static Map<String, Bitmap> mCachedBitmaps;
public VideoThumbnailLoader(Context context, ImageView imageView, int type) {
mContext = context;
mImageView = imageView;
mThumbnailType = type;
if(mCachedBitmaps == null) {
mCachedBitmaps = new HashMap<>();
}
}
@Override
protected Bitmap doInBackground(Uri... params) {
mPath = Utils.getPath(mContext, params[0]);
//if was loaded previously, return cached bitmap
if(mCachedBitmaps.containsKey(mPath)) {
return mCachedBitmaps.get(mPath);
} else {
return ThumbnailUtils.createVideoThumbnail(mPath, mThumbnailType);
}
}
@Override
protected void onPostExecute(Bitmap result) {
mImageView.setImageBitmap(result);
mCachedBitmaps.put(mPath, result);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment