获取视频的缩略图工具类

来源:互联网 发布:mac好玩的小游戏 编辑:程序博客网 时间:2024/06/04 18:03
/** * 获取视频的缩略图工具 * Created by Administrator on 2016/11/11. */public class GetVideoPicUtil {    /*    * 获取本地视频的缩略图    * */    //获取本地视频一张图片    public Bitmap getLocalVideoPic(String videoUrl_new) {        File file = new File(videoUrl_new);        Bitmap bitmap = null;        //获取第一帧图片,预览使用        if (file.length() != 0) {            MediaMetadataRetriever media = new MediaMetadataRetriever();            media.setDataSource(videoUrl_new);            bitmap = media.getFrameAtTime();                   }        return bitmap;    }    /*    * 获取网络视频的缩略图    * */     /** 获取网络视频的一帧图片* */    Bitmap bp_new = null;    public Bitmap getPic(final String videoUrl_new) {        new Thread(new Runnable() {            @Override            public void run() {                Bitmap bp = null;                bp = createVideoThumbnail(videoUrl_new, 640, 360);                bp_new = bp;            }        }).start();return bp_new;    }    //从网络获取视频缩略图    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)    private Bitmap createVideoThumbnail(String url, int width, int height) {        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) {                    } catch (RuntimeException ex) {                   } finally {            try {                retriever.release();            } catch (RuntimeException ex) {                           }        }        if (kind == MediaStore.Images.Thumbnails.MICRO_KIND && bitmap != null) {            bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height,                    ThumbnailUtils.OPTIONS_RECYCLE_INPUT);        }        return bitmap;    }}
0 0
原创粉丝点击