其他分享
首页 > 其他分享> > android – 写入sdCard时权限被拒绝

android – 写入sdCard时权限被拒绝

作者:互联网

我正在尝试将文件写入SDCard,但我在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)

在这里我的代码写入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(...) {
    ...
}

表现:

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

解决方法:

要写入Sdcard,您需要在清单文件中授予权限

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

标签:android,permission-denied,permissions
来源: https://codeday.me/bug/20191001/1840342.html