【问题标题】:How can I get my Android device Internal Download Folder path? [duplicate]如何获取我的 Android 设备内部下载文件夹路径? [复制]
【发布时间】:2014-02-12 10:08:59
【问题描述】:

可以获取安卓设备内部下载文件夹路径吗?

【问题讨论】:

标签: android download storage internals


【解决方案1】:

如果设备有 SD 卡,则使用:

Environment.getExternalStorageState() 

如果您没有 SD 卡,请使用:

Environment.getDataDirectory()

如果没有SD卡,可以在设备本地创建自己的目录。

    //if there is no SD card, create new directory objects to make directory on device
        if (Environment.getExternalStorageState() == null) {
                        //create new file directory object
            directory = new File(Environment.getDataDirectory()
                    + "/RobotiumTestLog/");
            photoDirectory = new File(Environment.getDataDirectory()
                    + "/Robotium-Screenshots/");
            /*
             * this checks to see if there are any previous test photo files
             * if there are any photos, they are deleted for the sake of
             * memory
             */
            if (photoDirectory.exists()) {
                File[] dirFiles = photoDirectory.listFiles();
                if (dirFiles.length != 0) {
                    for (int ii = 0; ii <= dirFiles.length; ii++) {
                        dirFiles[ii].delete();
                    }
                }
            }
            // if no directory exists, create new directory
            if (!directory.exists()) {
                directory.mkdir();
            }

            // if phone DOES have sd card
        } else if (Environment.getExternalStorageState() != null) {
            // search for directory on SD card
            directory = new File(Environment.getExternalStorageDirectory()
                    + "/RobotiumTestLog/");
            photoDirectory = new File(
                    Environment.getExternalStorageDirectory()
                            + "/Robotium-Screenshots/");
            if (photoDirectory.exists()) {
                File[] dirFiles = photoDirectory.listFiles();
                if (dirFiles.length > 0) {
                    for (int ii = 0; ii < dirFiles.length; ii++) {
                        dirFiles[ii].delete();
                    }
                    dirFiles = null;
                }
            }
            // if no directory exists, create new directory to store test
            // results
            if (!directory.exists()) {
                directory.mkdir();
            }
        }// end of SD card checking

在您的 manifest.xml 上添加权限

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

【讨论】:

  • 我刚刚在我的设备上创建了目录,没有使用 SD 卡,但是我的设备上没有显示目录
  • 我也有这个问题。
  • 这个答案非常令人困惑。当问题是针对内部存储时,为什么外部存储给出了答案?当问题只是针对目录的位置时,为什么答案是一堆代码?我找到了这个答案来寻找 INTERNAL 下载文件夹的位置。答案不应该像 /path/to/folder 吗?
猜你喜欢
  • 2020-08-17
  • 2020-08-18
  • 2020-02-05
  • 1970-01-01
  • 2014-05-29
  • 2019-01-17
  • 2016-04-12
  • 2015-03-01
  • 1970-01-01
相关资源
最近更新 更多