java - 外部存储中的 Android/Java File.mkdirs() 不工作

标签 java android linux android-sdcard

首先,我想说明我确实有

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

在我的 list 中指定,我确实检查了 Environment.MEDIA_MOUNTED。

在我看来,真正奇怪的是它返回 true,但实际上并没有创建目录。

public static void downloadFiles(ArrayList<FileList> list) {

    for (FileList file: list) {
        try {
            // This will be the download directory
            File download = new File(downloadDirPatch.getCanonicalPath(), file.getPath());

            // downloadDirPatch is defined as follows in a different class:
            //
            // private static String updateDir = "CognitionUpdate";
            // private static File sdcard = Environment.getExternalStorageDirectory();
            // final public static File downloadDir = new File(sdcard, updateDir);
            // final public static File downloadDirPatch = new File(downloadDir, "patch");
            // final public static File downloadDirFile = new File(downloadDir, "file");

            if (DEV_MODE)
                Log.i(TAG, "Download file: " + download.getCanonicalPath());

            // Check if the directory already exists or not
            if (!download.exists())
                // The directory doesn't exist, so attempt to create it
                if (download.mkdirs()) {
                    // Directory created successfully
                    Download.download(new URL(file.getUrl() + file.getPatch()), file.getPath(), file.getName(), true);
                } else {
                    throw new ExternalStorageSetupFailedException("Download sub-directories could not be created");
                }
            else {
                // Directory already exists
                Download.download(new URL(file.getUrl() + file.getPatch()), file.getPath(), file.getName(), true);
            }
        } catch (FileNotFoundException fnfe) {
            fnfe.printStackTrace();
        } catch (IOException ie) {
            ie.printStackTrace();
        } catch (ExternalStorageSetupFailedException essfe) {
            essfe.printStackTrace();
        }
    }
}

"if (download.mkdirs())"返回 true,但是当应用真正开始下载文件时它会抛出一个

FileNotFoundException: open failed: ENOENT (No such file or directory)

异常,后来我在手机上查看目录时,它不存在。

在程序的早期,应用程序设置了父下载目录,使用 File.mkdir() 一切正常,但 File.mkdirs() 对我来说似乎无法正常工作。

最佳答案

您的问题没有提供有关 FileNotFoundException 的详细信息。检查触发它的路径。忘记您认为路径是什么,记录它或通过调试器运行它以查看它的真实情况。

根据未正确创建的目录,(用你的眼睛)确认路径确实是你认为的。我看到您已经在记录 download.getCanonicalPath,请检查您的日志中它到底是什么。

最后,Download.download 真的在您认为可以保存的地方保存内容吗?在调用它之前,您正在使用 download 准备和验证目录,但是当您调用 Download.download 时,您并没有使用 download,所以这是不可能的。

顺便说一句,不要重复你自己,你可以在不重复 Download.download 行的情况下重写:

        if (!download.exists())
            if (!download.mkdirs()) {
                throw new ExternalStorageSetupFailedException("Download sub-directories could not be created");
            }
        }
        Download.download(new URL(file.getUrl() + file.getPatch()), file.getPath(), file.getName(), true);

关于java - 外部存储中的 Android/Java File.mkdirs() 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16631560/

相关文章:

java - 用 Ant build : dynamic build options?

android - Android Studio 2.3.1 错误:(155) Original attribute defined here,

linux - 构建机器上的 WebDriver

python - 如何在 ubuntu 20.04 上安装 python3-pip

java - 如何单击 span 标签内的元素? - Selenium

java - 获取 ClassCastException : java. lang.Integer cannot be cast to java.lang.String 错误而无需强制转换

java - Procrun 找不到服务的注册表项

android - java.lang.IllegalAccessError : Class ref in pre-verified class resolved to unexpected implementation getting while running test project?

android - Android 设备上的并发声音

linux - Alt 推送时 Manjaro Xfce 中基于 Electron 的应用程序的焦点丢失