2

I am trying to write a MIDI file on SDcard. It works on my device but not in the emulator. Here's the error on the logCat :

12-25 09:18:27.733: W/ExternalStorage(1951): Error create /sdcard/download/test.mid
12-25 09:18:27.733: W/ExternalStorage(1951): java.io.FileNotFoundException: /sdcard/download/test.mid
12-25 09:18:27.733: W/ExternalStorage(1951):    at org.apache.harmony.luni.platform.OSFileSystem.open(OSFileSystem.java:244)
12-25 09:18:27.733: W/ExternalStorage(1951):    at java.io.FileOutputStream.<init>(FileOutputStream.java:97)
12-25 09:18:27.733: W/ExternalStorage(1951):    at java.io.FileOutputStream.<init>(FileOutputStream.java:168)
12-25 09:18:27.733: W/ExternalStorage(1951):    at java.io.FileOutputStream.<init>(FileOutputStream.java:147)

I have set the permission in the manifest file, and change the path from "/sdcard/download/test.mid" to Environment.getExternalStorageDirectory()+"/download/test.mid" but still doesn't work.

FYI, I use Kevin Boone's MidiFile class, and here's my code :

String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        mf.writeToFile(Environment.getExternalStorageDirectory()+"/download/test.mid");
    }
fat fat
  • 23
  • 1
  • 3

2 Answers2

2

Well first make sure you have the proper permission, if you don't add this to you're AndroidManifest.xml

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

Secondly you should also check if download folder exist if not you have to create it. Check the method exist from the File class.

If you take a look at Kevin's MidiFile method use it only create a file on a specific path it does not ensure the path exist that is your job

mg.writeToFile ("somefile.mid");
Necronet
  • 6,704
  • 9
  • 49
  • 89
1

I would suggest checking if you have emaulator's sdcard is created with permissions to write on it.

Refer this: Android Emulator sdcard push error: Read-only file system

Once you are through with this step, I think it shall work for you programatically as well.

Community
  • 1
  • 1
Sumit
  • 291
  • 2
  • 2