8

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>
Cœur
  • 37,241
  • 25
  • 195
  • 267
zaamm
  • 178
  • 2
  • 2
  • 8

5 Answers5

9

For writing to the Sdcard you need to give the permission in your manifest file

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Ram kiran Pachigolla
  • 20,897
  • 15
  • 57
  • 78
4

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

Ryan
  • 2,240
  • 17
  • 17
0

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

twlkyao
  • 14,302
  • 7
  • 27
  • 44
Hades
  • 3,916
  • 3
  • 34
  • 74
0

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.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
-3
  1. Check that your directory fun and the file itisfun.txt exists 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.
  2. Make sure that your permission is outside of the <application> tag, usually before it.
twlkyao
  • 14,302
  • 7
  • 27
  • 44