I'm trying write a file into SDCard, but I am getting error in logcat:
01-24 09:03:33.647: W/System.err(3353): java.io.FileNotFoundException: /mnt/sdcard/fun/itisfun.txt: open failed: EACCES (Permission denied)
01-24 08:24:28.007: W/System.err(3353): Caused by: libcore.io.ErrnoException: open failed: EACCES (Permission denied)
01-24 09:03:33.756: W/System.err(3353):at libcore.io.Posix.open(Native Method)
And here my code to write into SDCard:
File root = null;
try {
// check for SDcard
root = Environment.getExternalStorageDirectory();
Log.i(TAG,"path.." +root.getAbsolutePath());
//check sdcard permission
if (root.canWrite()){
File fileDir = new File(root.getAbsolutePath()+"/fun/");
fileDir.mkdirs();
File file = new File(fileDir, "itisfun.txt");
FileWriter filewriter = new FileWriter(file);
BufferedWriter out = new BufferedWriter(filewriter);
out.write("I m enjoying......dude");
out.close();
}
} catch(...) {
...
}
Manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<permission android:name="android.permission.INTERNET"></permission>
Try browsing in the root folder of your device to see if there is another shortcut leading to your external SD card. Other alternatives would be using MTP (via USB cable) which is supported by SyncBackPro/SE V7 and newer, rooting the phone and/or using another ROM version.
Generally Access Denied error message is related to permissions. If you do not have sufficient permissions to view the file/folder/drive you get this error message. I suggest you to try to take the ownership of this drive and check if it helps.
If you can't write to SD Card on Android, then there is a slight chance that SD Card protection has been enabled. This prevents you from writing any new data into the SD Card. To disable this feature, follow the steps below: Remove the SD Card which is currently in read only state from your device.
For writing to the Sdcard you need to give the permission in your manifest file
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
You need to make sure you have the permission @Ram mentions, and the SD Card is mounted. You can check if it is mounted by:-
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()))
You should handle an unmounted card gracefully, but a common gotcha is if your phone is plugged in via the USB cable you may have it mounted via your desktop OS, which means it's not mounted by Android.
Thanks,
Ryan
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With