Permission denied when writing into sdCard

耗尽温柔 提交于 2019-11-28 03:25:08

问题


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>

回答1:


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

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



回答2:


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




回答3:


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




回答4:


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.




回答5:


  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.


来源:https://stackoverflow.com/questions/14475951/permission-denied-when-writing-into-sdcard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!