zaamm
zaamm

Reputation: 178

Permission denied when writing into sdCard

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>

Upvotes: 8

Views: 22577

Answers (5)

Gabe Sechan
Gabe Sechan

Reputation: 93728

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.

Upvotes: 0

twlkyao
twlkyao

Reputation: 14628

  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.

Upvotes: -3

Hades
Hades

Reputation: 3936

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

Upvotes: 0

Ryan
Ryan

Reputation: 2260

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

Upvotes: 4

Ram kiran Pachigolla
Ram kiran Pachigolla

Reputation: 21201

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

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

Upvotes: 9

Related Questions