2

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???

4

2 に答える 2

3

マニフェストに正しいアクセス許可があると仮定すると、これは機能するはずです。

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();
}
于 2013-01-25T03:45:30.853 に答える
1

「/」が欠落しているため、次のコードに変更する必要があります。

myFile = new File(path,"/mysdfile.xml");

ただし、マニフェストファイルに外部ストレージに書き込むためのアクセス許可が必要であることに注意してください。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
于 2013-01-25T02:34:55.423 に答える