Creating thumbnail from video file returns null bitmap
Asked Answered
H

4

7

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?

Headset answered 11/4, 2013 at 14:43 Comment(1)
Hi, I hope you solved this. Please share how. Me too facing the same issueEfflux
P
4

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

FFmpegMediaMetadataRetriever mmr = new FFmpegMediaMetadataRetriever();
mmr.setDataSource(intent.getDataString());
Bitmap b = mmr.getFrameAtTime();
mmr.release();
Procyon answered 30/12, 2013 at 20:30 Comment(0)
A
3

As I remember, argument filePath from createVideoThumbnail should be a classic filepath, not URI.

...

Uri videoUri = intent.getData();
final String realFilePath = getRealPathFromUri();
Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(realFilePath, MediaStore.Video.Thumbnails.MICRO_KIND);
...

public String getRealPathFromURI(final Uri contentURI) {
    Cursor cursor = getContentResolver().query(contentURI, null, null, null, null);
    if (cursor == null) { // Source is Dropbox or other similar local file path
        return contentURI.getPath();
    } else {
        cursor.moveToFirst();
        int idx = cursor.getColumnIndex(MediaStore.MediaColumns.DATA);
        if ( idx == -1 ) {
            return contentURI.getPath();
        }
        String rvalue =  cursor.getString(idx);
        cursor.close();
        return rvalue;
    }
}

EDIT:

Based on the source code of createVideoThumbnail, I created another implementation:

public static Bitmap createVideoThumbnail(Context context, Uri uri, int kind) {
    Bitmap bitmap = null;
    MediaMetadataRetriever retriever = new MediaMetadataRetriever();
    try {
        retriever.setMode(MediaMetadataRetriever.MODE_CAPTURE_FRAME_ONLY);
        retriever.setDataSource(context, uri);
        bitmap = retriever.captureFrame();
    } 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,
                ThumbnailUtils.TARGET_SIZE_MICRO_THUMBNAIL,
                ThumbnailUtils.TARGET_SIZE_MICRO_THUMBNAIL,
                ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
    }
    return bitmap;
}
Aeon answered 30/12, 2013 at 10:6 Comment(1)
I found out that if I dont give it a path to where I want to save the video I get back a ContentProvider Uri where I can then use this method to get the thumbnail MediaStore.Video.Thumbnails.getThumbnail(getContentResolver(), id, MediaStore.Video.Thumbnails.MICRO_KIND, options); but I want to specify the path so that my users can find the videos easilyHeadset
E
1

Use this file "mediaFile" and convert it into URI

      Uri uri=Uri.fromFile(mediaFile);

Then pass that URI in the below method. This is working fine at my side.

Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(uri.getPath(), MediaStore.Video.Thumbnails.MICRO_KIND);

Hope this will help you.

Executor answered 15/9, 2013 at 17:20 Comment(0)
P
0

I faced this problem and solved it this way:

  1. Create FileUtils Class which find path of file for you (I can't find reference of the class so I created a gist)

    String correctedUri = FileUtils.getPath(mContext, Uri.parse(localUri));
    
  2. Use following code

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

EDITED : See this solution it has better performance and easier.

Pitiless answered 1/12, 2015 at 19:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.