gpt4 book ai didi

android - 在 Android 上将视频录制到外部 SD 卡

转载 作者:行者123 更新时间:2023-11-29 00:50:23 25 4
gpt4 key购买 nike

我正在尝试将视频录制到外部 SD 卡。但是,每次我尝试准确地记录数据时——我总是会遇到 java.io.FileNotFound 异常。我想知道是否有人知道任何教程或可以帮助更正我的代码。

这是尝试录制视频的类

public class VideoActivity extends Activity {

private SurfaceView preview;
private SurfaceHolder previewHolder;
private String locationName;
private String filepath;
private File video;

public void onCreate(Bundle videocawk) {
super.onCreate(videocawk);
setContentView(R.layout.video_layout);
setSurface();
locationName = getIntent().getStringExtra("locationName");
filepath = getFilePath(locationName);
try {
MediaRecorder r = getMediaRecorder(filepath, previewHolder
.getSurface());
setSurfaceCallback(preview,r);
setButtonListeners(r);
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

private String getFilePath(String locName) {
String dir = Environment.getExternalStorageDirectory().getAbsolutePath();
String add = "/test/data/video/";
String name = locName + "--1";
String total = dir + add + name;
video = new File(total);
return total;
}

private void setSurface() {
preview = (SurfaceView) findViewById(R.id.preview);
previewHolder = preview.getHolder();
previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

private void setButtonListeners(final MediaRecorder r) {
Button start = (Button) findViewById(R.id.start_video);
Button end = (Button) findViewById(R.id.stop_video);

start.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
startRecording(r);

}
});

end.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
stopRecording(r);
setPassPrefs();
startActivity(setPassPrefs());
finish();

}
});

}

private void setSurfaceCallback(SurfaceView s, final MediaRecorder r)
{


SurfaceHolder.Callback surfaceCallback = new SurfaceHolder.Callback() {
public void surfaceCreated(SurfaceHolder holder) {

try {
r.setPreviewDisplay(previewHolder.getSurface());
} catch (Throwable t) {
Log.e("PictureDemo-surfaceCallback",
"Exception in setPreviewDisplay()", t);
Toast.makeText(VideoActivity.this, t.getMessage(),
Toast.LENGTH_LONG).show();
}
}

public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {

}

public void surfaceDestroyed(SurfaceHolder holder) {
r.stop();
r.release();

}
};

previewHolder.addCallback(surfaceCallback);
}

private Intent setPassPrefs() {
AttachedImageAdapter adapter = new AttachedImageAdapter(locationName,
VideoActivity.this);
adapter.setVideoPath(filepath);
Intent i = new Intent(VideoActivity.this, EnterTag.class);
i.putExtras(getIntent());
return i;

}

private void startRecording(MediaRecorder r) {
r.start();
}

private void stopRecording(MediaRecorder r) {
r.stop();
}

private MediaRecorder getMediaRecorder(String filepath, Surface s)
throws IllegalStateException, IOException {
MediaRecorder m_recorder = new MediaRecorder();
m_recorder.setPreviewDisplay(s);
m_recorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
m_recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
m_recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
m_recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
m_recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
m_recorder.setMaxDuration(20000); // length of video in MS
m_recorder.setVideoSize(320, 240);
m_recorder.setVideoFrameRate(15);
m_recorder.setOutputFile(video.getPath());
m_recorder.prepare();

return m_recorder;
}

如有任何帮助,我们将不胜感激——再次提前致谢。另外,这是一个 pastebin具有视频 Activity 引用的布局。

Here是我的错误日志的 PasteBin——如果有帮助的话。

最佳答案

我能想到的两件事:

1:你的SD卡可能没有挂载。

if (android.os.Environment.getExternalStorageState() != android.os.Environment.MEDIA_MOUNTED) then you have a problem.

2:文件路径中的所有目录可能都不存在。

filepath = getFilePath(locationName);
File file = new File(filepath);
File parentDir = file.getParentFile();
if (!parentDir.exists())
{
parentDir.mkdirs();
}

关于android - 在 Android 上将视频录制到外部 SD 卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3225763/

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