VideoView截取某一时刻播放截图以及网络图片转换成Bitmap

最近做VideoView播放视频时需要获取某一时刻的视频截图,实现如下:

需要注意的是setDataSource时,如果是网络视频,需要使用setDataSource(url,HashMap)方法

/**
     * 获取当前videoView截图
     */
    public static Bitmap getCurrentVideoBitmap(String url,VideoView videoView){
        Bitmap bitmap = null;
        MediaMetadataRetriever retriever = new MediaMetadataRetriever();

        try {
            retriever.setDataSource(url,new HashMap<String, String>());
            bitmap = retriever.getFrameAtTime(videoView.getCurrentPosition() * 1000); //取得指定时间的Bitmap,即可以实现抓图(缩略图)功能
        } 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 (bitmap == null) {
            return null;
        }

        //bitmap = Bitmap.createScaledBitmap(bitmap, 200, 200, true);
        bitmap = Bitmap.createBitmap(bitmap);
        return bitmap;
}

网络图片转换成Bitmap

public static Bitmap netImgToBitmap(String url) {
        URL mUrl = null;
        Bitmap bitmap = null;
        try {
            mUrl = new URL(url);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        try {
            HttpURLConnection conn = (HttpURLConnection) mUrl.openConnection();
            conn.setDoInput(true);
            conn.connect();
            InputStream inputStream = conn.getInputStream();
            bitmap = BitmapFactory.decodeStream(inputStream);
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bitmap;
    }



你可能感兴趣的:(bitmap,VideoView,网络图片,视频截图)