JasJar
JasJar

Reputation: 332

Android File not created on SD card

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)

Upvotes: 2

Views: 3626

Answers (4)

JasJar
JasJar

Reputation: 332

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.

Upvotes: 0

stealthjong
stealthjong

Reputation: 11093

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()?

Upvotes: 1

Mohsen Afshin
Mohsen Afshin

Reputation: 13436

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";);

Upvotes: 0

Sumant
Sumant

Reputation: 2795

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");

   }

Upvotes: 1

Related Questions