我一直在寻找这个问题。 它实际上可以在旧版本的 android 中运行,但是在我更新 SDK 后,出现错误。 错误消息为“打开的文件:ENOTDIR(不是目录):/sdcard/PlayNumbers/mysdfile.xml” 有人可以指出我做错了什么吗? 我的代码如下。
非常感谢,
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();
附:好吧,我已经改变了,正如你们提到的,就像我的代码一样,但它仍然给我同样的错误,它不是目录......有什么想法吗???
最佳答案
假设您在 list 中拥有正确的权限,这应该可行:
File externalStorageDir = Environment.getExternalStorageDirectory();
File playNumbersDir = new File(externalStorageDir, "PlayNumbers");
File myFile = new File(playNumbersDir, "mysdfile.xml");
if (!playNumbersDir.exists()) {
playNumbersDir.mkdirs();
}
if(!myFile.exists()){
myFile.createNewFile();
}
关于java - 在Android中向SD卡写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14514363/