当前位置:操作系统 > 安卓/Android >>

android 获取视频,图片缩略图

1、获取视频缩略图有两个方法(1)通过内容提供器来获取(2)人为创建缩略图

(1)缺点就是必须更新媒体库才能看到最新的视频的缩略图

[java]
/** 
     * @param context 
     * @param cr
     * @param Videopath
     * @return 
     */ 
    public static Bitmap getVideoThumbnail(Context context, ContentResolver cr, String Videopath) {  
            ContentResolver testcr = context.getContentResolver();  
            String[] projection = { MediaStore.Video.Media.DATA, MediaStore.Video.Media._ID, };  
            String whereClause = MediaStore.Video.Media.DATA + " = '" + Videopath + "'";  
            Cursor cursor = testcr.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, projection, whereClause,  
                            null, null);  
            int _id = 0;  
            String videoPath = "";  
            if (cursor == null || cursor.getCount() == 0) {  
                    return null;  
            }  
            if (cursor.moveToFirst()) {  
 
                    int _idColumn = cursor.getColumnIndex(MediaStore.Video.Media._ID);  
                    int _dataColumn = cursor.getColumnIndex(MediaStore.Video.Media.DATA);  
                    do {  
                            _id = cursor.getInt(_idColumn);  
                            videoPath = cursor.getString(_dataColumn);  
                    } while (cursor.moveToNext());  
            }  
            cursor.close(); 
            BitmapFactory.Options options = new BitmapFactory.Options();  
            options.inDither = false;  
            options.inPreferredConfig = Bitmap.Config.RGB_565;  
            Bitmap bitmap = MediaStore.Video.Thumbnails.getThumbnail(cr, _id, Images.Thumbnails.MINI_KIND,  
                            options);  
            return bitmap;  
    } 

/**
     * @param context
     * @param cr
     * @param Videopath
     * @return
     */
    public static Bitmap getVideoThumbnail(Context context, ContentResolver cr, String Videopath) {
            ContentResolver testcr = context.getContentResolver();
            String[] projection = { MediaStore.Video.Media.DATA, MediaStore.Video.Media._ID, };
            String whereClause = MediaStore.Video.Media.DATA + " = '" + Videopath + "'";
            Cursor cursor = testcr.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, projection, whereClause,
                            null, null);
            int _id = 0;
            String videoPath = "";
            if (cursor == null || cursor.getCount() == 0) {
                    return null;
            }
            if (cursor.moveToFirst()) {

                    int _idColumn = cursor.getColumnIndex(MediaStore.Video.Media._ID);
                    int _dataColumn = cursor.getColumnIndex(MediaStore.Video.Media.DATA);
                    do {
                            _id = cursor.getInt(_idColumn);
                            videoPath = cursor.getString(_dataColumn);
                    } while (cursor.moveToNext());
            }
            cursor.close();
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inDither = false;
            options.inPreferredConfig = Bitmap.Config.RGB_565;
            Bitmap bitmap = MediaStore.Video.Thumbnails.getThumbnail(cr, _id, Images.Thumbnails.MINI_KIND,
                  &n

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,