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

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

概述1、获取视频缩略图有两个方法(1)通过内容提供器来获取(2)人为创建缩略图(1)缺点就是必须更新媒体库才能看到最新的视频的缩略图

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,String VIDeopath) {
            ContentResolver testcr = context.getContentResolver();
            String[] projection = { MediaStore.VIDeo.Media.DATA,};
            String whereClause = MediaStore.VIDeo.Media.DATA + " = '" + VIDeopath + "'";
            Cursor cursor = testcr.query(MediaStore.VIDeo.Media.EXTERNAL_CONTENT_URI,
                            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,
                            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 kind){
  Bitmap bitmap = null;
  bitmap = thumbnailUtils.createVIDeothumbnail(vIDeoPath,kind);
  bitmap = thumbnailUtils.extractthumbnail(bitmap,thumbnailUtils.OPTIONS_RECYCLE_input);
  return bitmap;
    }

2、图片缩略图

[java]
复制代码 代码如下:
/**
    * 
    * @param context
    * @param cr
    * @param Imagepath
    * @return
    */
   public static Bitmap getimagethumbnail(Context context,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, 
                           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, 
                           options); 
           return bitmap; 
   }

 /**
     *
     * @param context
     * @param cr
     * @param Imagepath
     * @return
     */
    public static Bitmap getimagethumbnail(Context context,String Imagepath) {
            ContentResolver testcr = context.getContentResolver();
            String[] projection = { MediaStore.Images.Media.DATA,};
            String whereClause = MediaStore.Images.Media.DATA + " = '" + Imagepath + "'";
            Cursor cursor = testcr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,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,
                            options);
            return bitmap;
    }

总结

以上是内存溢出为你收集整理的android 获取视频,图片缩略图的具体实现全部内容,希望文章能够帮你解决android 获取视频,图片缩略图的具体实现所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1142532.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存