从视频文件创建缩略图返回null位图

从视频文件创建缩略图返回null位图

本文介绍了从视频文件创建缩略图返回null位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发送一个意图发动摄像机

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();
    }

我把一个视频,它得到正确地存储,当的onActivityResult 解雇我使用的意图,以获得 URI 其中,其存储创建位图

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

这是URI 的示例文件:///storage/emulated/0/Pictures/BCA/VIDEO_20131227_145043.mp4

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

但位为空每次。如此以来,文档说可能返回null如果视频损坏或不支持的格式我检查该目录中的视频,它起着罚款加上其在 .MP4 这是支持这样的文件我究竟做错了什么?

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?

推荐答案

我记得参数文件路径从 createVideoThumbnail 应该是一个经典的文件路径,而不是 URI

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;
    }
}

编辑:

,我创造了另一个实现:

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;
}

这篇关于从视频文件创建缩略图返回null位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 19:11