Stack Overflow archive
0 score

How to get thumbnail for video in my /sdcard/Android/data/mypackage/files folder?

score
0
question views
64.7K
license
CC BY-SA 3.0

Here is a similar answer to Matthew Willis but with added reflection. Why? because science.

java
/**
 *
 * @param path
 *            the path to the Video
 * @return a thumbnail of the video or null if retrieving the thumbnail failed.
 */
public static Bitmap getVidioThumbnail(String path) {
    Bitmap bitmap = null;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
        bitmap = ThumbnailUtils.createVideoThumbnail(path, Thumbnails.MICRO_KIND);
        if (bitmap != null) {
            return bitmap;
        }
    }
    // MediaMetadataRetriever is available on API Level 8 but is hidden until API Level 10
    Class<?> clazz = null;
    Object instance = null;
    try {
        clazz = Class.forName("android.media.MediaMetadataRetriever");
        instance = clazz.newInstance();
        final Method method = clazz.getMethod("setDataSource", String.class);
        method.invoke(instance, path);
        // The method name changes between API Level 9 and 10.
        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD) {
            bitmap = (Bitmap) clazz.getMethod("captureFrame").invoke(instance);
        } else {
            final byte[] data = (byte[]) clazz.getMethod("getEmbeddedPicture").invoke(instance);
            if (data != null) {
                bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
            }
            if (bitmap == null) {
                bitmap = (Bitmap) clazz.getMethod("getFrameAtTime").invoke(instance);
            }
        }
    } catch (Exception e) {
        bitmap = null;
    } finally {
        try {
            if (instance != null) {
                clazz.getMethod("release").invoke(instance);
            }
        } catch (final Exception ignored) {
        }
    }
    return bitmap;
}

Originally posted on Stack Overflow. Public user contributions are licensed under Creative Commons Attribution-ShareAlike.