获取视频缩略图

之前用的一直都是
    /**  
        * 获取视频缩略图  
        * @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.x系统下可用,API LEVEL > 14时却只能返回null


通过上网搜索发现http://www.tuicool.com/articles/uqmUFfY:

import android.media.MediaMetadataRetriever; 
import android.provider.MediaStore.Images;
import java.util.HashMap;
import android.os.Build; 

       /**  
        * 获取视频缩略图  
        * @param videoPath  
        * @param width  
        * @param height  
        * @param kind  
        * @return  
        */    
    private Bitmap createVideoThumbnail(String url, int width, int height,int kind) {
        Bitmap bitmap = null;
        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        //int kind = MediaStore.Video.Thumbnails.MINI_KIND;
        try {
          if (Build.VERSION.SDK_INT >= 14) {
            retriever.setDataSource(url, new HashMap<String, String>());
          } else {
            retriever.setDataSource(url);
          }
          bitmap = retriever.getFrameAtTime();
        } catch (IllegalArgumentException ex) {
          // Assume this is a corrupt video file
        } catch (RuntimeException ex) {
          // Assume this is a corrupt video file.
        } finally {
          try {
            retriever.release();
          } catch (RuntimeException ex) {
            // Ignore failures while cleaning up.
          }
        }
        if (kind == Images.Thumbnails.MICRO_KIND && bitmap != null) {
          bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height,
              ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
        }
        return bitmap;
  }
完美解决!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值