7

我发送了启动摄像机的意图

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其存储位置以创建位图

这是 uri 的一个例子file:///storage/emulated/0/Pictures/BCA/VIDEO_20131227_145043.mp4

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

但位图每次都是空的。因此,由于文档说May return null if the video is corrupt or the format is not supported我检查了目录中的视频,并且它播放良好,而且它的.mp4文件受支持,所以我在这里做错了什么?

4

4 回答 4

4

您可以尝试MediaMetadataRetrieverFFmpegMediaMetadataRetriever。这是一个例子:

FFmpegMediaMetadataRetriever mmr = new FFmpegMediaMetadataRetriever();
mmr.setDataSource(intent.getDataString());
Bitmap b = mmr.getFrameAtTime();
mmr.release();
于 2013-12-30T20:30:18.160 回答
3

我记得,filePath来自createVideoThumbnail的参数应该是经典文件路径,而不是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;
    }
}

编辑:

基于 的源代码createVideoThumbnail,我创建了另一个实现:

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;
}
于 2013-12-30T10:06:41.923 回答
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.

于 2013-09-15T17:20:22.780 回答
0

我遇到了这个问题并以这种方式解决了它:

  1. 创建为您找到文件路径的FileUtils类(我找不到该类的引用,所以我创建了一个要点)

    String correctedUri = FileUtils.getPath(mContext, Uri.parse(localUri));
    
  2. 使用以下代码

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

已编辑: 请参阅解决方案,它具有更好的性能和更容易。

于 2015-12-01T19:32:06.640 回答