android convert video file to image

public void getBitmapsFromVideo() {
        String dataPath = Environment.getExternalStorageDirectory() + "/11211015_1555.mp4";
        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        retriever.setDataSource(dataPath);
        // Get the length of the video (in milliseconds)
        String time = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
        // Get the length of the video (in seconds)
        int seconds = Integer.valueOf(time) / 1000;
        // Get the bitmap of each second, such as the first second, the second second
        for (int i = 1; i <= seconds; i++) {
            Bitmap bitmap = retriever.getFrameAtTime(i * 1000 * 1000, MediaMetadataRetriever.OPTION_CLOSEST_SYNC);
            String path = Environment.getExternalStorageDirectory() + File.separator+"_car_vedio_capture"+ File.separator + i + ".jpg";
            saveBitmap(bitmap,path);
        }

    }



public void saveBitmap(Bitmap bm, String path) {
        String dir = path.substring(0,path.lastIndexOf("/"));
        File dirFile =  new File(dir);
        if (!dirFile.exists()) {
            dirFile.mkdirs ();
        }
        File f = new File(path);
        if (f.exists()) {
            f.delete();
        }
        FileOutputStream out = null;
        try {
            out = new FileOutputStream(f);
            bm.compress(Bitmap.CompressFormat.JPEG, 100, out);
        } catch (FileNotFoundException e) {
            e.printStackTrace ();
        } finally {
            if (out != null) {
                try {
                    out.flush();
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace ();
                }
            }
        }
    }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326412816&siteId=291194637