欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

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

时间:2020-10-08 13:26:39|栏目: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,
                            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;
    }

上一篇:iOS中给UITableView的侧滑删除增加多个按钮的实现方法

栏    目:Android代码

下一篇:Android获取手机的版本号等信息的代码

本文标题:android 获取视频,图片缩略图的具体实现

本文地址:http://www.codeinn.net/misctech/8388.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有