Creating thumbnail from video file returns null bitmap

tyczj picture tyczj · Apr 11, 2013 · Viewed 8.2k times · Source

I send an intent to launch the video camera

PackageManager pm = getPackageManager();
    if(pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)){
            Intent video = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
            File tempDir= new File(Environment.getExternalStoragePublicDirectory(
                      Environment.DIRECTORY_PICTURES), "BCA");
            if(!tempDir.exists())
            {
                if(!tempDir.mkdir()){
                    Toast.makeText(this, "Please check SD card! Image shot is impossible!", Toast.LENGTH_SHORT).show();
                }
            }

                String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",Locale.US).format(new Date());
                File mediaFile = new File(tempDir.getPath() + File.separator +
                "VIDEO_"+ timeStamp + ".mp4");
                Uri videoUri = Uri.fromFile(mediaFile);
                video.putExtra(MediaStore.EXTRA_OUTPUT, videoUri);
                video.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
                startActivityForResult(video, VIDEO_REQUEST);

    }else{
        Toast.makeText(this, "This device does not have a rear facing camera",Toast.LENGTH_SHORT).show();
    }

i take a video and it gets store correctly, When the onActivityResult get fired I use the intent to get the uri where its stored to create the bitmap

this is an example of the uri file:///storage/emulated/0/Pictures/BCA/VIDEO_20131227_145043.mp4

 Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(intent.getDataString(), MediaStore.Video.Thumbnails.MICRO_KIND);

but the bitmap is null everytime. So since the docs say May return null if the video is corrupt or the format is not supported I check the video in the directory and it plays fine plus its a .mp4 file which is supported so what am I doing wrong here?

Answer

William Seemann picture William Seemann · Dec 30, 2013

You can try MediaMetadataRetriever or FFmpegMediaMetadataRetriever. Here is an example:

FFmpegMediaMetadataRetriever mmr = new FFmpegMediaMetadataRetriever();
mmr.setDataSource(intent.getDataString());
Bitmap b = mmr.getFrameAtTime();
mmr.release();