6

This question is actually asked and supposedly answered here: android get video thumbnail PATH, not Bitmap.

I've tried several times but can't get it to work. I always get a null returned. Any help on this please?

Edit: The sample code I'm using now:

public static String getVideoThumbnailPath(Context context,
        String filePath) {
    String thubmnailPath;
    String where = Video.Thumbnails.VIDEO_ID
            + " In ( select _id from video where _data =?)";
    final String[] VIDEO_THUMBNAIL_TABLE = new String[] { Video.Media._ID, // 0
            Video.Media.DATA, // 1
    };
    Uri videoUri = MediaStore.Video.Thumbnails.getContentUri("external");

    Cursor c = context.getContentResolver().query(videoUri,
            VIDEO_THUMBNAIL_TABLE, where, new String[] { filePath }, null);

    if ((c != null) && c.moveToFirst()) {
        thubmnailPath = c.getString(1);
        c.close();
        Log.i(TAG, "thumb path: " + thubmnailPath);
        return thubmnailPath;
    } else {
        c.close();
        Log.i(TAG, "thumb path is null");
        return null;
    }
}
Community
  • 1
  • 1
Nii Laryea
  • 1,211
  • 5
  • 18
  • 31

1 Answers1

14

First, you need to know the content uri of the file. If you have the file path, this shows you how to get the content uri.

public static String[] thumbColumns = { MediaStore.Video.Thumbnails.DATA };
public static String[] mediaColumns = { MediaStore.Video.Media._ID };

public static String getThumbnailPathForLocalFile(Activity context,
        Uri fileUri) {

    long fileId = getFileId(context, fileUri);

    MediaStore.Video.Thumbnails.getThumbnail(context.getContentResolver(),
            fileId, MediaStore.Video.Thumbnails.MICRO_KIND, null);

    Cursor thumbCursor = null;
    try {

        thumbCursor = context.managedQuery(
                MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI,
                thumbColumns, MediaStore.Video.Thumbnails.VIDEO_ID + " = "
                        + fileId, null, null);

        if (thumbCursor.moveToFirst()) {
            String thumbPath = thumbCursor.getString(thumbCursor
                    .getColumnIndex(MediaStore.Video.Thumbnails.DATA));

            return thumbPath;
        }

    } finally {
    }

    return null;
}

public static long getFileId(Activity context, Uri fileUri) {

    Cursor cursor = context.managedQuery(fileUri, mediaColumns, null, null,
            null);

    if (cursor.moveToFirst()) {
        int columnIndex = cursor
                .getColumnIndexOrThrow(MediaStore.Video.Media._ID);
        int id = cursor.getInt(columnIndex);

        return id;
    }

    return 0;
}

Reference: http://androidcodezs.blogspot.com/2013/10/android-thumbnail-from-video.html

Community
  • 1
  • 1
Nii Laryea
  • 1,211
  • 5
  • 18
  • 31