您好我一直在尝试使用意图录制并保存Nougat 7.0中的视频,我可以录制视频,但不会保存在设备存储中。我甚至使用FileProvider
来避免' FileUriExposedException'。但是当捕获图像时,它将被保存在下面指定的路径中。
这是我的代码。
private Uri imageUri;
private File imageFile = null;
public File videoFilePath() {
return new File(getDefaultCameraPath(), "Video_" + System.currentTimeMillis() + ".mp4");
}
private void callCameraIntent(){
Intent cameraIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
imageFile = videoFilePath();
imageUri = FileProvider.getUriForFile(CreatePostActivity.this, BuildConfig.APPLICATION_ID + ".provider", imageFile);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(cameraIntent, 2);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != Activity.RESULT_CANCELED) {
//Here it returns imageFile does not exist so it skips the if statement
if (resultCode == Activity.RESULT_OK && requestCode == 2 && imageFile != null && imageFile.exists()) {
}
}
}
以上代码适用于所有pre-Nougat
版本。任何人都可以为我提供更好的解决方案来录制视频并保存在设备存储中。
答案 0 :(得分:1)
这是解决方案!
如果您的targetSdkVersion为24或更高,我们必须使用FileProvider类来访问特定文件或文件夹,以使其可供其他应用访问。
Stackoverflow链接: android.os.FileUriExposedException: file:///storage/emulated/0/test.txt exposed beyond app through Intent.getData()
你也应该检查所需的权限android.Manifest.permission.CAMERA
答案 1 :(得分:0)
File Video_folder = new File(Environment.getExternalStorageDirectory(),
"Sample/Videos")
File videoMediaFile=null;
private void DispatchVideoRecordIntent() {
try {
videoMediaFile = File.createTempFile(
"VID_" + System.currentTimeMillis(),
".mp4",
Video_folder
);
Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
takeVideoIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(videoMediaFile));
startActivityForResult(takeVideoIntent, REQUEST_VIDEO_CAPTURE);
} catch (Exception e) {
Log.e("Image Capturing", e.toString());
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == REQUEST_VIDEO_CAPTURE) {
if (!TextUtils.isEmpty(videoMediaFile.getAbsolutePath())) {
// videoMediaFile.getAbsolutePath() is your file path
} else {
Toast.makeText(this, "Unable to capture video.", Toast.LENGTH_SHORT).show();
}
}