I have been searching this problem.
It actually worked in older version of android, but after I updated SDK, I get an error.
Error message is “open filed: ENOTDIR (Not a directory): /sdcard/PlayNumbers/mysdfile.xml”
Can please someone point me what I did wrong??
My codes are below.
Many Thanks,
path=new File("/sdcard/PlayNumbers");
myFile = new File(path,"mysdfile.xml");
if (!path.exists()) {
path.mkdirs();
}
if(!myFile.exists()){
myFile.createNewFile();
}
FileOutputStream fOut = new FileOutputStream(myFile);
OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
myOutWriter.append("test");
myOutWriter.close();
fOut.close();
==>
File path = null;
File myFile = null;
String filePath = Environment.getExternalStorageDirectory().toString();
path=new File(filePath+"/PlayNumbers/");
myFile = new File(path,"mysdfile.xml");
//i also tried both as below
//path=new File(filePath+"/PlayNumbers");
//myFile = new File(path,"mysdfile.xml");
if (!path.exists()) {
path.mkdirs();
}
if(!myFile.exists()){
myFile.createNewFile();
}
FileOutputStream fOut = new FileOutputStream(myFile);
OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
myOutWriter.append("test");
myOutWriter.close();
fOut.close();
p.s. ok, I have changed as you guys mentioned like my code, but it still gives me same error that it is not directory… any idea???
you just need to change to the following code because you missing “/”:
But keep in mind that you have to have the permission for writing in external storage in your manifest file :