gpt4 book ai didi

使用nexus 7从相机进行的Android视频记录返回空数据

转载 作者:太空狗 更新时间:2023-10-29 14:17:55 26 4
gpt4 key购买 nike

在我的应用程序中,我具有从相机录制视频的功能。该代码适用于所有设备,但使用 nexus-7 (android OS 4.3) 我得到 null onActivityResult(int requestCode, int resultCode, Intent data) 方法。

当我调试我的代码时,我注意到 nexus-7 (android OS 4.3) 的 Intent 数据在这里为空

请提出一些解决方案。

这里是recordVideo方法的代码

  private void recordVideo() {
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);


// set video quality
// 1- for high quality video
Intent intent = new Intent(android.provider.MediaStore.ACTION_VIDEO_CAPTURE);


// start the video capture Intent
startActivityForResult(intent, CAMERA_CAPTURE_VIDEO_REQUEST_CODE);
}

最佳答案

请按如下方式修改您的 recordView 方法:-

private void recordVideo() {
intent = new Intent(android.provider.MediaStore.ACTION_VIDEO_CAPTURE);
fileUri = getOutputMediaFile(MEDIA_TYPE_VIDEO); // create a file to save the video in specific folder (this works for video only)
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); // set the video image quality to high

// start the Video Capture Intent
startActivityForResult(intent, REQUEST_VIDEO_CAPTURED_NEXUS);

}

//然后在onActivityResult方法中

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {

if (resultCode == Activity.RESULT_OK) {
switch (requestCode) {
case REQUEST_VIDEO_CAPTURED_NEXUS:
this.videoFromCameraNexus(resultCode, data);
break;

default:
break;
}


}
}

//videoFromCameraNexus 方法

private void videoFromCameraNexus(int resultCode, Intent data) {

if(fileUri != null) {
Log.d(TAG, "Video saved to:\n" + fileUri);
Log.d(TAG, "Video path:\n" + fileUri.getPath());
Log.d(TAG, "Video name:\n" + getName(fileUri));
// use uri.getLastPathSegment() if store in folder
//use the file Uri.
}
}
Get the output Media file uri with the following Method

public Uri getOutputMediaFile(int type)
{
// To be safe, you should check that the SDCard is mounted

if(Environment.getExternalStorageState() != null) {
// this works for Android 2.2 and above
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES), "SMW_VIDEO");

// This location works best if you want the created images to be shared
// between applications and persist after your app has been uninstalled.

// Create the storage directory if it does not exist
if (! mediaStorageDir.exists()) {
if (! mediaStorageDir.mkdirs()) {
Log.d(TAG, "failed to create directory");
return null;
}
}

// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
if(type == MEDIA_TYPE_VIDEO) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"VID_"+ timeStamp + ".mp4");
} else {
return null;
}

return Uri.fromFile(mediaFile);
}

return null;
}

希望对您有所帮助。

关于使用nexus 7从相机进行的Android视频记录返回空数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19854081/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com