android 获取视频,图片缩略图的具体实现

时间:2023-07-28 00:20:39 

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,
                            options);
            return bitmap;
    }(2)人为创建缩略图要耗费一点时间
[java]


/**
    * 获取视频缩略图
    * @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;
   }


 /**
     * 获取视频缩略图
     * @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]


/**
    * 
    * @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; 
   }


 /**
     *
     * @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;
    }

标签:视频,图片,缩略图
0
投稿

猜你喜欢

  • c# 制作gif的四种方法

    2023-03-17 20:01:08
  • c#用for语句输出一个三角形的方法

    2023-12-17 05:46:53
  • Android 中Volley二次封装并实现网络请求缓存

    2023-09-17 06:16:35
  • C# MVC 微信支付教程系列之公众号支付代码

    2022-11-29 09:14:29
  • Java中Optional的正确用法与争议点详解

    2023-11-12 03:50:21
  • ADO.NET实体数据模型详细介绍

    2023-10-16 12:15:41
  • 关于工厂方法模式的Java实现

    2021-12-20 18:06:13
  • Android布局之绝对布局AbsoluteLayout详解

    2023-07-26 07:44:27
  • C++ 风靡一时的连连看游戏的实现流程详解

    2022-10-08 13:39:12
  • 浅谈C#跨线程调用窗体控件(比如TextBox)引发的线程安全问题

    2022-03-16 06:35:41
  • Unity使用物理引擎实现多旋翼无人机的模拟飞行

    2021-09-25 04:11:40
  • Flutter实现手势识别功能详解方法

    2022-12-21 15:41:41
  • Android解析XML文件升级APK的方法

    2022-06-05 20:47:26
  • Android实现聊天界面

    2023-04-09 22:57:57
  • C#生成MD5的函数代码

    2021-08-04 14:50:24
  • Android编程实现简单文件浏览器功能

    2022-01-02 06:13:46
  • 详解Springboot分布式限流实践

    2021-07-12 14:29:18
  • C# TextBox 扩展方法数据验证详细说明

    2022-05-07 14:21:36
  • C#实现保存文件时重名自动生成新文件的方法

    2022-07-02 15:01:57
  • Java容器类源码详解 Deque与ArrayDeque

    2021-10-15 12:43:21
  • asp之家 软件编程 m.aspxhome.com