文件mkdirs()方法不是在Android的工作/ java的是在、文件、方法、工作

2023-09-04 11:07:32 作者:内心独白

我已经退出这个我的头发有一段时间了。下面的方法应该下载一个文件,并将其保存到硬盘驱动器上的指定位置。

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;
    }
}

此外,如果该文件不存在,则假定让目录中的文件。 (如果有另一个文件已在这个位置,应该只是没有做任何事情)。然而,由于某些原因,该mkdirs()方法从不的目录。我用尽了一切从这些括号,明确使得父文件类,似乎没有任何工作。我相当肯定,该驱动器是可写的,因为在那之后已经确定这只是所谓的,也就是通过它运行时的调试后,真实的。因此,该方法将失败,因为父目录都不能进行。谁能告诉我,如果有什么问题的方式我打电话呢?

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

感谢您

推荐答案

如果你正在写的内容到你的SD卡希望你在添加 android.permission.WRITE_EXTERNAL_STORAG​​E 许可您的清单

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