【问题标题】:Unable to create file - EACCESS Permission Denied无法创建文件 - EACCESS 权限被拒绝
【发布时间】:2013-07-11 03:28:33
【问题描述】:

我正在将一些文本写入文件,但在写入文本之前,我无法创建文件本身。

File newxmlfile = new File(
                        Environment.getExternalStorageDirectory() + "new.xml");
                XmlSerializer serializer = Xml.newSerializer();
                try {
                    newxmlfile.createNewFile();
                } catch (Exception e) {
                    Log.e("IOException", "exception in createNewFile() method",
                            e);
                }

权限设置正常。

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

SDCard 也是可写和挂载的。

但是,我一直得到

07-11 11:23:01.134: E/IOException(5363): exception in createNewFile() method
07-11 11:23:01.134: E/IOException(5363): java.io.IOException: open failed: EACCES (Permission denied)
07-11 11:23:01.134: E/IOException(5363):    at java.io.File.createNewFile(File.java:948)
07-11 11:23:01.134: E/IOException(5363):    at com.app.example.MainActivity$LongOperation.doInBackground(MainActivity.java:71)
07-11 11:23:01.134: E/IOException(5363):    at com.app.example.MainActivity$LongOperation.doInBackground(MainActivity.java:1)
07-11 11:23:01.134: E/IOException(5363):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
07-11 11:23:01.134: E/IOException(5363):    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
07-11 11:23:01.134: E/IOException(5363):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
07-11 11:23:01.134: E/IOException(5363):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
07-11 11:23:01.134: E/IOException(5363):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
07-11 11:23:01.134: E/IOException(5363):    at java.lang.Thread.run(Thread.java:856)
07-11 11:23:01.134: E/IOException(5363): Caused by: libcore.io.ErrnoException: open failed: EACCES (Permission denied)
07-11 11:23:01.134: E/IOException(5363):    at libcore.io.Posix.open(Native Method)
07-11 11:23:01.134: E/IOException(5363):    at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
07-11 11:23:01.134: E/IOException(5363):    at java.io.File.createNewFile(File.java:941)
07-11 11:23:01.134: E/IOException(5363):    ... 8 more
07-11 11:23:01.134: E/FileNotFoundException(5363): can't create FileOutputStream

【问题讨论】:

  • 代替这个Environment.getExternalStorageDirectory() + "\new.xml")试试这个Environment.getExternalStorageDirectory() +File.Seperator+ "new.xml")

标签: android file-io android-permissions


【解决方案1】:

你的代码是正确的,你只需要在文件名前多加一个/(或File.separator)如下:

File newxmlfile = new File(
        Environment.getExternalStorageDirectory() + "/new.xml");
XmlSerializer serializer = Xml.newSerializer();
try {
    newxmlfile.createNewFile();
} catch (Exception e) {
    Log.e("IOException", "exception in createNewFile() method",
            e);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多