录制视频在Android上外接SD卡 [英] Record Video to External SD Card on Android

查看:176
本文介绍了录制视频在Android上外接SD卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想录制视频到外接SD卡。然而,每次我试图实际上可以记录数据 - 我总是得到一个java.io.FileNotFound例外。我想知道是否有人知道的任何教程或可以帮助纠正我的code。

I am trying to record video to an external SD card. However, everytime I try and acutally record the data -- I always get a java.io.FileNotFound exception. I was wondering if anyone knew of any tutorials or could help correct my code.

下面是它试图录制视频类

Here is the class in which tries to record video

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;
}

}

任何帮助将是AP preciated - 并再次感谢提前。此外,这里是一个引擎收录具有布局的视频活动引用。

Any help would be appreciated -- and thanks again in advance. Also, here is a pastebin that has the layout that the video activity references.

这里是我的错误日志的引擎收录 - 如果它帮助。

Here is the PasteBin of my error log -- if it helps.

推荐答案

有两件事情我能想到的:

Two things I can think of:

1:你的SD卡可以不安装

1: Your SD Card may not be mounted.

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

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

2: All of the directories in path to the file may not exist.

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

这篇关于录制视频在Android上外接SD卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆