Skip to content

Instantly share code, notes, and snippets.

@benbaxter
Last active August 21, 2017 21:19
Show Gist options
  • Save benbaxter/69cf49f369e6ff46c53bcf6d1ea31bf0 to your computer and use it in GitHub Desktop.
Save benbaxter/69cf49f369e6ff46c53bcf6d1ea31bf0 to your computer and use it in GitHub Desktop.
Loads a bitmap from an async task.
@Override
public void getThumbnail(int index, ResultCallback callback) {
LoadBitmapAsyncTask task = new LoadBitmapAsyncTask(index, callback);
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
class LoadBitmapAsyncTask extends AsyncTask<Void, Void, Bitmap> {
final int mIndex;
final ResultCallback mResultCallback;
public LoadBitmapAsyncTask(int index, ResultCallback resultCallback) {
mIndex = index;
mResultCallback = resultCallback;
}
@Override
protected Bitmap doInBackground(Void... voids) {
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource(mVideoUrl, new HashMap<>());
long position = getSeekPositions()[index];
Log.d("SeekProvider", "position: " + position);
Bitmap thumbnail = retriever.getFrameAtTime(position,
MediaMetadataRetriever.OPTION_CLOSEST);
retriever.release();
return thumbnail;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
mResultCallback.onThumbnailLoaded(bitmap, mIndex);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment