Leif Andersen
Leif Andersen

Reputation: 22342

File mkdirs() method not working in android/java

I've been pulling out my hair on this for a while now. The following method is supposed to download a file, and save it to the location specified on the hard drive.

private static void saveImage(Context context, boolean backgroundUpdate, URL url, File file) {

    if (!Tools.checkNetworkState(context, backgroundUpdate))
        return;

    // Get the image
    try {
        // Make the file
        file.getParentFile().mkdirs();

        // Set up the connection
        URLConnection uCon = url.openConnection();
        InputStream is = uCon.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(is);

        // Download the data
        ByteArrayBuffer baf = new ByteArrayBuffer(50);
        int current = 0;
        while ((current = bis.read()) != -1) {
                baf.append((byte) current);
        }

        // Write the bits to the file
        OutputStream os = new FileOutputStream(file);
        os.write(baf.toByteArray());
        os.close();
    } catch (Exception e) {
        // Any exception is probably a newtork faiilure, bail
        return;
    }
}

Also, if the file doesn't exist, it is supposed to make the directory for the file. (And if there is another file already in that spot, it should just not do anything). However, for some reason, the mkdirs() method never makes the directory. I've tried everything from explicit parentheses, to explicitly making the parent file class, and nothing seems to work. I'm fairly certain that the drive is writable, as it's only called after that has already been determined, also that is true after running through it while debugging. So the method fails because the parent directories aren't made. Can anyone tell me if there is anything wrong with the way I'm calling it?

Also, if it helps, here is the source for the file I'm calling it in:

https://github.com/LeifAndersen/NetCatch/blob/master/src/net/leifandersen/mobile/android/netcatch/services/RSSService.java

Thank you

Upvotes: 11

Views: 10302

Answers (4)

Jonathan Ben-Avraham
Jonathan Ben-Avraham

Reputation: 4841

Further to Kevin Day's reply, there is another failure modality that previous posts did not mention. It is that even if you have android.permission.WRITE_EXTERNAL_STORAGE in your manifest, if you are trying to write to media such as /mnt/sdcard that is mounted externally as a USB gadget (i.e. you connected your device as an external disk to your PC with a USB cable) at the time you are trying to do the write, then you can't write to the /mnt/sdcard file system. In this case FILE.mkdir will return false. Obviously, if you are not checking the return code you wont see this problem until you get an IOException if you try to create a file in the directory that you think that you created. And if you don't create a file in this non-existent directory then the app will just silently fail.

I make this mistake constantly when debugging Android apps - forget that I can't both have the sdcard mounted externally and at the same time have my app write to it.

Upvotes: 5

fargath
fargath

Reputation: 7884

Please check with this, it may help you..

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

enter tis permission in your manifest file.

Upvotes: 13

DeRagan
DeRagan

Reputation: 22930

If you are writing contents to your SD card hope you have added android.permission.WRITE_EXTERNAL_STORAGE permission in your manifest

Upvotes: 30

Kevin Day
Kevin Day

Reputation: 16413

Are you sure that mkdirs is failing? Why don't you check the return value of the mkdirs call and log an error message if it returns false?

if (!file.getParentFile().exists() && !file.getParentFile().mkdirs()){
  log("Unable to create " + file.getParentFile());
}

I suspect that you might be surprised with what the file was...

PS - and please do something with that error handler - at minimum, log the error. Catching Exception and doing a silent return is horrible practice.

Upvotes: 3

Related Questions