1

I have mounted an external sd card (7.9GB). Following is the code I am using to transfer a raw audio file from my project to the sdcard. I am using JellyBean 4.2 version. I am able to achieve this using a fileManager app. So the sdcard definitely is writable.

        File storagedir = new File("/mnt/extsd");
        if (storagedir.isDirectory()) {
        String[] dirlist = storagedir.list();
        for (int i = 0; i < dirlist.length; i++) {
            System.out.println(dirlist[i]);
        }
        File file = new File(storagedir, "Audio.mp3");

        try {
            InputStream is = getResources().openRawResource(R.raw.audio);
            OutputStream os = new FileOutputStream(file);
            byte[] data = new byte[is.available()];
            is.read(data);
            os.write(data);
            is.close();
            os.close();
            Toast.makeText(getApplicationContext(), "Saved!", Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
        }

Manifest:

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

But I get the File not found exception:

          java.io.FileNotFoundException: mnt/extsd/Audio.mp3 openfailed:
          EACCES (Permission Denied)
Susheel
  • 784
  • 1
  • 12
  • 20

2 Answers2

2

You ideally should not be using such hardcoded paths like that. You should be using the strings coming from http://developer.android.com/reference/android/os/Environment.html .

The main reason for this is because these strings CAN change and its up to the platform to return the correct values.

JoxTraex
  • 13,423
  • 6
  • 32
  • 45
  • @ Jox Traex: I understand that. In this case, the file path is valid. I am checking the names of directories under that path using sysout if you notice. – Susheel Jan 30 '15 at 02:46
  • Thing is I have multiple external storage directories. The mount path for my external sdcard is /mnt/extsd. Without this string, how can I give a generic Environment.getExternalStorageDirectory() command? How will the app know where in the external storage to put the file? – Susheel Jan 30 '15 at 02:53
  • Please don't try to assume you will know the platform LET the platform tell you what the path is. Not sure you should worry about that. – JoxTraex Jan 30 '15 at 02:56
  • I just switched "/mnt/extsd' to Environment.getExternalStorageDirectory().getAbsolutePath(). Now, the file is being saved to my internal sdcard which is not what I wanted. I wanted to specifically save to my removable sdcard. I am able to navigate this external card using filemanager app. So I thought this was a do-able process programatically. – Susheel Jan 30 '15 at 03:02
  • 1
    Write access to a removable micro SD card is not possible on Kitkat. Here is a way to get the path to removable storage: http://stackoverflow.com/a/24336742/1048340 – Jared Rummler Jan 30 '15 at 03:06
  • @JaredRummler: Thanks, I'm using Jelly Bean 4.2 though. – Susheel Jan 30 '15 at 03:09
0

So, it turns out I made a really silly mistake. The path should have been:

               File storagedir = new File("/mnt/extsd/");

   I missed the 2nd backslash after extsd. 
Susheel
  • 784
  • 1
  • 12
  • 20