Reputation: 784
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)
Upvotes: 1
Views: 522
Reputation: 784
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.
Upvotes: 0
Reputation: 13541
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.
Upvotes: 2