Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 795
zaamm Avatar asked Jan 23 '13 09:01

zaamm


People also ask

How do I fix permission denied SD card?

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.

Why does SD card Say Access Denied?

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.

What does it mean when it says can't write in SD card?

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.


2 Answers

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

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
like image 133
Ram kiran Pachigolla Avatar answered Oct 13 '22 00:10

Ram kiran Pachigolla


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

like image 22
Ryan Avatar answered Oct 13 '22 00:10

Ryan