简体   繁体   中英

Android create file exception EACCES (Permission denied)

I cannot write to the internal storage of an android device or emulator. I am able write to a file in the external storage directory.

I am getting this exception EACCES (Permission denied) on the createNewFile call.

WRITE_EXTERNAL_STORAGE permission is in the right place in my manifest

public static Boolean writeFile(String filename, String data)
{
      try 
      {
        File file = new File(filename);
        // If file does not exists, then create it
        if (!file.exists()) 
        {
            Log.d("MyLog","file doesn't exist, creating");
            file.getParentFile().mkdirs();
            file.createNewFile();
        }

        FileWriter fw = new FileWriter(file.getAbsoluteFile());
        BufferedWriter bw = new BufferedWriter(fw);
        bw.write(data);
        bw.close();

        //Log.d("Success","Success");
        return true;
      } 
      catch (IOException e) 
      {
        e.printStackTrace();
        Log.d("MyLog","writeFile fail, error:"+e.getMessage());
        return false;
      }
}

I have tried numerous paths:

filename=Environment.getRootDirectory()+"/"+"test.dat";
filename=Environment.getDataDirectory()+"/"+"test.dat";
filename="data/data/com.android.emsi/testnew.txt";

the only thing that will work is: filename=Environment.getExternalStorageDirectory()+"/"+"test.dat";

Try this

String filename = "myfile";
String string = "Hello world!";
FileOutputStream outputStream;

try {
outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
outputStream.write(string.getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM