开发者

Android VideoRecording error, cannot create path to file

开发者 https://www.devze.com 2023-01-12 07:41 出处:网络
java.lang.IOException, path to file cannot be created. my catch is working by dont know why is isnt being created?

java.lang.IOException, path to file cannot be created. my catch is working by dont know why is isnt being created?

Im not sure why im getting this error, i assumed the setOutputFile() would create the file ..

any help appreciated, as there are a few errors in DDMS

this is my viderecorder class:

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

this is my main activity

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/recordi开发者_运维百科ng");

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

    }
}


Did you add <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> to your AndroidManifest.xml file?

See Security and Permissions for more details.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号