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>
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
Here's a bit of code I use,
public void yourMethod(){
File root = getDir(getApplicationContext());
try {
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(...) {
...
}
}
public File getDir(Context context) {
if (android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED))
cacheDir = new File(
android.os.Environment.getExternalStorageDirectory(),
DIRECTORY_NAME);
else
cacheDir = context.getCacheDir();
return cacheDir;
}
If there is no external storage, it basically uses the phones internal cache (not good for large files)
Read this
If you're on 4.4, read here: http://www.androidcentral.com/kitkat-sdcard-changes Basically you can no longer read and write anywhere on the drive. You can only write to your private directory and directories you've become the owner of.
- Check that your directory
funand the fileitisfun.txtexists on the SDcard, if you want to make them by program, you have to add the permission:<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
This permission allows the application to create file or directory, the permission:<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />only allows the application to read and write the file that is already exist. - Make sure that your permission is outside of the
<application>tag, usually before it.
来源:https://stackoverflow.com/questions/14475951/permission-denied-when-writing-into-sdcard