2

I'm trying to create a file on the SD on my device. This worked a week ago, but now it isn't, and I don't understand why.

The Logcat prints:

java.io.FileNotFoundException ...pathtofile... (no such file or directory)

So, the file is not being created. I have the correct permissions on the android manifest:

  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

I create the file this way:

if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
           base = Environment.getExternalStorageDirectory().getAbsolutePath();
       }
   String fname = File.separator +"VID_"+ timeStamp + ".3gp";
       mediaFile = new File(base+fname);

Then I check if it exists:

   if(mediaFile.exists()){
           Log.v("mediaFile","ex");
       }else{
           Log.v("mediaFile","no ex");

       }

And the log says that IT DOESN'T EXIST. I also have tried with file.createNewFile() and it doesn't work.

So, a week ago it was working, now it doesn't work, it could be a problem with the SD card ???? Could it be some type of BUG!!!????

Thanks

EDIT: More Code

The function where the file is created is :

private static File getOutputMediaFile()

Called from:

private static Uri getOutputMediaFileUri(){
         return Uri.fromFile(getOutputMediaFile());
   }

And setted to mediarecorder output as:

vMediaRecorder.setOutputFile(getOutputMediaFileUri().toString());

So, when I do mediarecorder.prepare():

try {
            vMediaRecorder.prepare();

        } catch (IllegalStateException e) {
            Log.v("RELEASE VIDREC1",e.toString());
            releaseMediaRecorder();
            return false;
        } **catch (IOException e) {
            Log.v("RELEASE VIDREC2",e.toString());
            releaseMediaRecorder();
            return false;**
        }

The bold catch is the one that runs, and prints:

java.io.FileNotFoundException ...pathtofile... (no such file or directory)
JasJar
  • 336
  • 3
  • 14

4 Answers4

1

You merely create the object mediaFile, not the actual file. Use this:

if(!f.exists()){
  f.createNewFile();
}

I tried this, for me it works.

final String filename = "file.3gp";
final String path = Environment.getExternalStorageDirectory() + "/" + filename;
File outputfile = new File(path);
if (!outputfile.exists()) {
    try {
        outputfile.createNewFile();
    } 
    catch (IOException e) {
        e.printStackTrace();
    }
}
MediaRecorder recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(outputfile.toString());
try {
    recorder.prepare();
    recorder.start();
    /*recorder.stop();
    recorder.reset();
    recorder.release();*/
} 
catch (IllegalStateException e) {
    e.printStackTrace();
} 
catch (IOException e) {
    e.printStackTrace();
}

Try and see whether or not it works. If not, can you add the full code of getOutputMediaFile()?

stealthjong
  • 10,858
  • 13
  • 45
  • 84
1

just try this

String fname = "VID_"+ timeStamp + ".3gp";

if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
   mediaFile = new File(android.os.Environment.getExternalStorageDirectory(),fname);
    if(!mediaFile.exists())
    {
         mediaFile.createNewFile();
    }
}

if(mediaFile.exists()){
       Log.v("mediaFile","ex");
   }else{
       Log.v("mediaFile","no ex");

   }
Sumant
  • 2,775
  • 2
  • 22
  • 30
  • Ok thanks, now the Logcat says that the file exists, but the problem is that I'm sending it as outputfile to a Mediarecorder.setoutputfile(uri.tostring()), where uri is the path to the file. When I do mediarecorder.prepare, the io exception prints there is no file with that name :S. – JasJar Oct 18 '12 at 09:22
0

My answer here:

First open the path, then add the file:

String dir = Environment.getExternalStorageDirectory(); // getAbsolutePath is not requried
File path = new File(dir);
File root = new File(path,  "VID_"+ timeStamp + ".3gp";);
Community
  • 1
  • 1
Mohsen Afshin
  • 13,273
  • 10
  • 65
  • 90
  • Ok tried this but modifying String dir = Environment.getExternalStorageDirectory().toString(); I have the same result that I had with Sumant answer (see my comment). – JasJar Oct 18 '12 at 09:31
0

Thanks for your help, I've solved the problem using old code. It is strange because the "file saving" part has no modification. Thanks for all guys, kinda.

JasJar
  • 336
  • 3
  • 14