Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

android - 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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...