0

I am trying to write a new wave file to the Ringtones folder on external storage. But unfortunately the file is not being created.

I have followed the android docs on how to do this, and have implemented it the way it is suggested.

I have also added the proper permissions in manifest. As well, I have checked that my device external storage is readable/writable.

The following code writes the log statement "directory not created". As well, any subsequent calls to stream cause nullPointer.

public WavFileOutStream(String fileName)
{

    // Get the directory for the user's public pictures directory. 
    File file = new File(Environment.getExternalStoragePublicDirectory(
             Environment.DIRECTORY_RINGTONES), fileName);
    if (!file.getParentFile().mkdirs()) {
         Log.e("trace", "Directory not created");
    }

    try {
        stream = new DataOutputStream(new FileOutputStream(file));

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 

}
user3341858
  • 127
  • 2
  • 10
  • 1
    Is the directory Ringtones already present in your file system ? – Kakarot Feb 24 '14 at 20:04
  • Yes directory ringtones is already present. – user3341858 Feb 24 '14 at 20:08
  • In that case mkdir() will return false....refer http://docs.oracle.com/javase/7/docs/api/java/io/File.html#mkdir() – Kakarot Feb 24 '14 at 20:09
  • Yes I forgot to add other reasons why I think it is not successfully being opened. I edited initial post. I get nullpointer exception to any other call to DataOutputStream stream. – user3341858 Feb 24 '14 at 20:13
  • Try file.createFile() and then create the OutputStream' – Kakarot Feb 24 '14 at 20:19
  • The only method available was file.createNewFile(). I tried that with the same results. Thank you for the suggestion. – user3341858 Feb 24 '14 at 20:25
  • Do have a wave file in your Directory..that you want to copy to Ringtones folder ? – Kakarot Feb 24 '14 at 20:27
  • The wave file exists in my Raw folder. The wave file is read in by program and the written out. I don't think that is important at the moment though. I does not matter what file I am writing out to the stream if the stream is no even opened. – user3341858 Feb 24 '14 at 20:30
  • do you have write permission in the manifest? – Blackbelt Feb 24 '14 at 20:31
  • can you try this FileUtils.copyFile(source, desc); – Kakarot Feb 24 '14 at 20:34
  • I am not sure exactly what FileUtils.copyFile(source, desc); does. But I don't want to copy one wave to another wave location. My program does significant processing and it is best to think that the wave file data is being 100% generated by the program. – user3341858 Feb 24 '14 at 21:08

2 Answers2

0

You are misinterpreting the meaning of the log message and its cause.

File.mkdirs() is documented to return:

  • true if the directory was created,

  • false on failure or if the directory already existed

As the ringtones folder presumably already exists, you would likely have the second case.

If you still have other reasons to believe that this is not working, edit any stack trace produced by the following catch block into the body of your question.

Chris Stratton
  • 39,853
  • 6
  • 84
  • 117
  • Yes any subsequent calls to the DataOutputStream stream cause nullPointer exception. This leads me to believe that file is not being opened. – user3341858 Feb 24 '14 at 20:10
  • Then you need to edit the stack trace produced by your catch block, or (if it is not failing there) the platform uncaught exception handler, into the body of your question. – Chris Stratton Feb 24 '14 at 20:49
  • It simply boils down to a nullpointer exception at whenever stream.anyMethod() is being called. – user3341858 Feb 24 '14 at 21:10
  • That's not informative. Perhaps you need to break the FileOutputStream constructor out into its own line so you can get a meaningful stack trace if that fails, and only if it does not then attempt to create a DataOutputStream from it. It would be good to log your intended path too, to see if it seems reasonable for the device. – Chris Stratton Feb 24 '14 at 21:16
  • How do I see the strack traces that are caught from a catch statement? – user3341858 Feb 24 '14 at 21:27
  • I have called .exists() and .canWrite() on the directory, and both returned true. The directory exists and is writable. – user3341858 Feb 24 '14 at 21:38
  • What about the file? You will see the stacktrace if you either do not catch the exception, or do an e.printStackTrace() in the catch block. – Chris Stratton Feb 24 '14 at 22:58
0

I often forget myself. Make sure you have this permission in your application manifest :)

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

Rastikan
  • 517
  • 5
  • 18