Android VideoRecording错误,无法创建文件路径

时间:2010-08-21 13:14:52

标签: java android

java.lang.IOException,无法创建文件路径。我的捕获工作不知道为什么没有被创造?

我不知道为什么我得到这个错误,我假设setOutputFile()会创建文件..

任何帮助表示感谢,因为DDMS中存在一些错误

这是我的视频录像课:

    package com.sg86.quickrecord;

    import java.io.File;
    import java.io.IOException;

    import android.media.MediaRecorder;
    import android.os.Environment;

    public class VideoRecorder {

  final MediaRecorder recorder = new MediaRecorder();
     final String path;

     /**
     * create a new video recording stored in SDcard Root
     */
 public VideoRecorder(String path) {
     this.path = organisePath(path);
     }

 private String organisePath(String path) {
  if (!path.startsWith("/")) {
   path = "/" + path;
     }
     if (!path.contains(".")) {
      path += ".3gp";
     }
     return Environment.getExternalStorageDirectory().getAbsolutePath() + path;
     } 
    public void start() throws IOException {
     String state = android.os.Environment.getExternalStorageState();
        if(!state.equals(android.os.Environment.MEDIA_MOUNTED))  {
            throw new IOException("SD Card is not mounted.  It is " + state + ".");
        }

        // make sure the directory we plan to store the recording in exists
        File directory = new File(path).getParentFile();
        if (!directory.exists() && !directory.mkdirs()) {
          throw new IOException("Path to file could not be created.");
        }
        recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
        recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
        recorder.setOutputFile(path);
        recorder.prepare();
        recorder.start();
    }
    public void stop() throws IOException {
        recorder.stop();
        recorder.release();
    }
 }

这是我的主要活动

package com.sg86.quickrecord;

import java.io.File;
import java.io.IOException;
import java.lang.IllegalStateException;

import android.app.Activity;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

@SuppressWarnings("unused")
public class QuickRecord extends Activity {

 public static final String WRITE_EXTERNAL_STORAGE = "android.permission.WRITE_EXTERNAL_STORAGE";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button recordBtn = (Button) findViewById(R.id.button01);
        Button stopBtn = (Button) findViewById(R.id.button02);
        final VideoRecorder record = new VideoRecorder("/QuickRecord/recording");

        recordBtn.setOnClickListener(new OnClickListener() {   
   public void onClick(View v) {
    // TODO Auto-generated method stub
    try {
     record.start();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
  });

        stopBtn.setOnClickListener(new OnClickListener() {   
   public void onClick(View v) {
    try {
     record.stop();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
  });

    }
}

1 个答案:

答案 0 :(得分:1)

您是否已将<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />添加到AndroidManifest.xml文件中?

有关详细信息,请参阅Security and Permissions