tags:

views:

63

answers:

1

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

    }
}
A: 

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

See Security and Permissions for more details.

JRL
i added this to the .java filepublic static final String WRITE_EXTERNAL_STORAGE = "android.permission.WRITE_EXTERNAL_STORAGE"lol, very noob i know but the android resources arent very clear at all!eclipse shows no proposals in the manifest which is a shame. that fixed that error thankyou! got a bunch of others now.i changed it about to record audio insted, added all the permissions and hardware features to the manifest and got it to record a clip to the right location! could the video issue be down to lack of a preview in my app? the error relates to SurfaceView so i assume so
sg86-
@Shaun: If you have another problem, post a separate question with the description of the error you are getting.
JRL
thankyou mate, ill work on it tomorrow, i was just playing with appwidget, how horrible is that!
sg86-