Android调用系统相机拍摄视频以及获取缩略图

1.调用相机拍摄视频:

 // 拍摄视频
                    Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
                    intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
                    // 录制视频最大时长15s
                    intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, 15);
                    startActivityForResult(intent, REQUEST_CODE_CAMERA);

2.获取返回的视频路径:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == REQUEST_CODE_CAMERA) {
                Uri uri = data.getData();
                Cursor cursor = this.getContentResolver().query(uri, null, null, null, null);
                if (cursor != null && cursor.moveToNext()) {
                    int id = cursor.getInt(cursor.getColumnIndex(MediaStore.Video.VideoColumns._ID));
                    // 视频路径
                    String filePath = cursor.getString(cursor.getColumnIndex(MediaStore.Video.VideoColumns.DATA));
                    // ThumbnailUtils类2.2以上可用  Todo 获取视频缩略图
                    Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(filePath, MediaStore.Images.Thumbnails.MICRO_KIND);
                    // 图片Bitmapfile
                    File file = CommonUtils.compressImage(bitmap);
                    // 保存成功后插入到图库,其中的file是保存成功后的图片path。这里只是插入单张图片
                    // 通过发送广播将视频和图片插入相册
                    sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)));

                    cursor.close();
                }
            }
        }
    }

你可能感兴趣的:(Android)