a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 系統 > Android > 正文

android 獲取視頻,圖片縮略圖的具體實現

2020-04-11 12:13:09
字體:
來源:轉載
供稿:網友

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 欧美综合第一页 | 古典武侠第一页久久777 | 欧美一区二区三区视频 | 久久久久91 | 久久黄色网 | 精品国产麻豆 | 亚洲精品一二三区 | 欧美激情自拍 | 国产精品久久久久久久久久久久久 | 久久精品一区二区三区四区 | 久久精品久久综合 | 99视频在线免费观看 | 伊人欧美视频 | 在线看黄色av | 天天操天天干天天爽 | 精品国产91| 亚洲日韩欧美一区二区在线 | 一区在线观看视频 | 亚洲毛片网站 | av免费观看网页 | 中文字幕日韩高清 | 久久亚洲精品国产精品紫薇 | 色就是色欧美 | 天堂影院一区二区 | 中文字幕免费在线 | www久| 日韩精品免费在线视频 | 青青久久 | 国产福利在线观看视频 | 欧美一区二区在线观看 | 性高湖久久久久久久久 | 欧美日韩在线视频一区二区 | www.亚洲| 97成人精品视频在线观看 | 成人免费网站www网站高清 | 一级黄色片欧美 | 欧美日韩久久 | 亚洲欧洲精品成人久久奇米网 | 成人在线欧美 | 韩日欧美| 经典法国性xxxx精品 |