博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android 获取视频,图片缩略图
阅读量:4036 次
发布时间:2019-05-24

本文共 2818 字,大约阅读时间需要 9 分钟。

1、获取视频缩略图有两个方法

(1)通过内容提供器来获取

(2)人为创建缩略图

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

获取的方法
/**  
     * @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;   
    }  
(2)人为创建缩略图要耗费一点时间
[java] view plaincopy
/** 
    * 获取视频缩略图 
    * @param videoPath 
    * @param width 
    * @param height 
    * @param kind 
    * @return 
    */  
   private Bitmap getVideoThumbnail(String videoPath, int width , int height, int kind){  
    Bitmap bitmap = null;  
    bitmap = ThumbnailUtils.createVideoThumbnail(videoPath, kind);  
    bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height, ThumbnailUtils.OPTIONS_RECYCLE_INPUT);  
    return bitmap;  
   }  
2、图片缩略图
[java] view plaincopy
/**  
    *   
    * @param context  
    * @param cr 
    * @param Imagepath  
    * @return  
    */  
   public static Bitmap getImageThumbnail(Context context, ContentResolver cr, String Imagepath) {   
           ContentResolver testcr = context.getContentResolver();   
           String[] projection = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID, };   
           String whereClause = MediaStore.Images.Media.DATA + " = '" + Imagepath + "'";   
           Cursor cursor = testcr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, whereClause,   
                           null, null);   
           int _id = 0;   
           String imagePath = "";   
           if (cursor == null || cursor.getCount() == 0) {   
                   return null;   
           }   
           if (cursor.moveToFirst()) {   
  
                   int _idColumn = cursor.getColumnIndex(MediaStore.Images.Media._ID);   
                   int _dataColumn = cursor.getColumnIndex(MediaStore.Images.Media.DATA);   
  
                   do {   
                           _id = cursor.getInt(_idColumn);   
                           imagePath = 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.Images.Thumbnails.getThumbnail(cr, _id, Images.Thumbnails.MINI_KIND,   
                           options);   
           return bitmap;   
   }  

转载地址:http://ryjdi.baihongyu.com/

你可能感兴趣的文章
JavaScript基础知识(2)
查看>>
转载一个webview开车指南以及实际项目中的使用
查看>>
android中对于非属性动画的整理
查看>>
一个简单的TabLayout的使用
查看>>
ReactNative使用Redux例子
查看>>
Promise的基本使用
查看>>
android给文字加边框(修改不能居中的问题)
查看>>
coursesa课程 Python 3 programming 统计文件有多少单词
查看>>
coursesa课程 Python 3 programming 输出每一行句子的第三个单词
查看>>
coursesa课程 Python 3 programming Dictionary methods 字典的方法
查看>>
Returning a value from a function
查看>>
coursesa课程 Python 3 programming Functions can call other functions 函数调用另一个函数
查看>>
coursesa课程 Python 3 programming The while Statement
查看>>
course_2_assessment_6
查看>>
coursesa课程 Python 3 programming course_2_assessment_7 多参数函数练习题
查看>>
coursesa课程 Python 3 programming course_2_assessment_8 sorted练习题
查看>>
在unity中建立最小的shader(Minimal Shader)
查看>>
1.3 Debugging of Shaders (调试着色器)
查看>>
关于phpcms中模块_tag.class.php中的pc_tag()方法的含义
查看>>
vsftp 配置具有匿名登录也有系统用户登录,系统用户有管理权限,匿名只有下载权限。
查看>>