如何获得视频缩略图路径,而不是位图 [英] How to get the video thumbnail path, and not the bitmap

查看:406
本文介绍了如何获得视频缩略图路径,而不是位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题是居然问,也应该回答此:<一href=\"http://stackoverflow.com/questions/14483682/android-get-video-thumbnail-path-not-bitmap\">android获得视频缩略图路径,而不是位图。

我试过几次,但不能让它开始工作。我总是得到一个空返回。任何帮助吗?

编辑:示例code我现在使用:

 公共静态字符串getVideoThumbnailPath(上下文的背景下,
        字符串文件路径){
    串thubmnailPath;
    字符串,其中= Video.Thumbnails.VIDEO_ID
            +在(从视频_id哪里_data =?);
    最终的String [] = VIDEO_THUMBNAIL_TABLE新的String [] {Video.Media._ID,// 0
            Video.Media.DATA,// 1
    };
    乌里VIDEOURI = MediaStore.Video.Thumbnails.getContentUri(外部);    光标C = context.getContentResolver()查询(VIDEOURI,
            VIDEO_THUMBNAIL_TABLE,在那里,新的String [] {}文件路径,NULL);    如果((C = NULL)及!&安培; c.moveToFirst()){
        thubmnailPath = c.getString(1);
        c.close();
        Log.i(TAG,拇指路径:+ thubmnailPath);
        返回thubmnailPath;
    }其他{
        c.close();
        Log.i(TAG,拇指路径为空);
        返回null;
    }
}


解决方案

首先,你需要知道文件的内容URI。如果你的文件路径,展示了如何获取内容的URI。

 公共静态的String [] = thumbColumns {MediaStore.Video.Thumbnails.DATA};
公共静态的String [] = mediaColumns {MediaStore.Video.Media._ID};公共静态字符串getThumbnailPathForLocalFile(活动的背景下,
        乌里了fileURI){    长FILEID = getFileId(背景下,fileURI所);    MediaStore.Video.Thumbnails.getThumbnail(context.getContentResolver(),
            FILEID,MediaStore.Video.Thumbnails.MICRO_KIND,NULL);    光标thumbCursor = NULL;
    尝试{        thumbCursor = context.managedQuery(
                MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI,
                thumbColumns,MediaStore.Video.Thumbnails.VIDEO_ID +=
                        + FILEID,NULL,NULL);        如果(thumbCursor.moveToFirst()){
            字符串thumbPath = thumbCursor.getString(thumbCursor
                    .getColumnIndex(MediaStore.Video.Thumbnails.DATA));            返回thumbPath;
        }    } {最后
    }    返回null;
}公共静态长getFileId(Activity上下文,乌里了fileURI){    光标光标= context.managedQuery(fileURI所,mediaColumns,NULL,NULL,
            空值);    如果(cursor.moveToFirst()){
        INT参数:columnIndex =光标
                .getColumnIndexOrThrow(MediaStore.Video.Media._ID);
        INT ID = cursor.getInt(参数:columnIndex);        返回ID;
    }    返回0;
}

参考:的http://android$c$czs.blogspot.com/2013/10/android-thumbnail-from-video.html

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;
    }
}

解决方案

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

这篇关于如何获得视频缩略图路径,而不是位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆