Reputation: 2342
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";
Upvotes: 0
Views: 866
Reputation: 292
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();
}
Upvotes: 0