简体   繁体   中英

Can't write file on SD-card (Is a directory)

I'm getting the following error when trying to write midi-file on the Android SD-Card:

12-14 16:22:22.219: ERROR/Thread writer(1108): java.io.FileNotFoundException: /mnt/sdcard/folder/midifiles/file.mid (Is a directory) in /mnt/sdcard/folder/midifiles/

That's the relevant code:

public void writeFile(String filename, String dir, int bpm) throws Exception {          

    File f = new File(dir,filename);    
    if(!f.exists()) {
        f.mkdirs();

        if(!f.createNewFile()) {
            return; 
        }
    }
    FileOutputStream fos = new FileOutputStream(f);

I'm receiving the path via

Environment.getExternalStorageDirectory()

The relevant permission has been included as well.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

The file-writer is located in an external project/library... if this is in any case useful to know.

I guess that this is kind of a simple mistake, but I honestly have no idea what went wrong here.

Thanks in advance.

You are creating the directory /mnt/sdcard/folder/midifiles/file.mid with

f.mkdirs();

You should just do:

File f = new File(dir);    
if(!f.exists()) {
    f.mkdirs();

    File f1 = new File(dir,filename);    
    if(!f1.createNewFile()) {
       return; 
    }
}

or just

File f = new File(dir,filename);    
if(!f.exist()) {
    f.createNewFile();
    return; 
}

Are you running this on your phone? or connected to your pc? because it may be looking on your pc.

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