【问题标题】:Save file to Specific folder on SD card (External Storage Removable)将文件保存到 SD 卡上的特定文件夹(可移动外部存储)
【发布时间】:2018-05-11 09:48:22
【问题描述】:

我尝试使用路径 context.getExternalFileDirs() 进行保存,但它会将文件保存到 SD 卡上的文件夹 /storage/9016-4EF8/Android/data/package/files。

File file = new File(context.getExternalFilesDir(null), myFolder);

我搜索了 Android 仅支持对 App 文件夹的读/写,如/Android/data/package/files,但我想将文件保存到特定文件夹,如 /storage/9016-4EF8/MyFolder。我怎样才能做到这一点?

【问题讨论】:

  • 你能添加你正在使用的代码吗?
  • 这已经是 sd 卡上的应用程序特定文件夹。所以这应该是可写的。 sd卡的其余部分不可使用文件系统写入。
  • 我需要一个将数据保存到特定文件夹的解决方案。因为这样 /Android/data/package/files 上的数据有问题,所以在用户更新应用或卸载/重新安装时会被删除。
  • 是的,我知道。没有更新。仅在卸载时。现在阅读我的 cmets 并回答,这样你就知道你必须做什么了。
  • 嗨@greenapps 我不确定案例用户更新应用程序。您确定更新不会删除应用数据吗?

标签: android


【解决方案1】:

如果您想写入整个 micro SD 卡,请使用 Storage Access Framework。

使用Intent.ACTION_OPEN_DOCUMENT_TREE 让您的应用用户选择卡片。

在 Android 7+ 上查看存储卷。

【讨论】:

  • 如果你有一个关于使用存储访问框架写入数据的示例非常好
  • 谷歌一下。有很多使用这种意图的例子。还有 Intent.ACTION_OPEN_DOCUMENT 和 Intent.ACTION_CREATE_DOCUMENT。全部尝试。
  • @YoungKing 已发布您的问题的答案 发表评论您需要更多帮助 完整的代码可以在我的网站上通过教程 androidstackoverflow.com 进行解释
【解决方案2】:

这里有一个链接,它解释了如何让用户选择内部或外部存储。您让用户选择路径并维护该静态变量,所有数据都将使用 THE_PATH 变量写入 SQLite DB

LINK

【讨论】:

    【解决方案3】:

    您可以使用Environment.getExternalStorageDirectory(),这将为您提供公共外部目录来读写文件。

    在 /storage/9016-4EF8/MyFolder/test.txt 中创建文本文件的示例代码

    File docsFolder = new File(Environment.getExternalStorageDirectory() + "/MyFolder");
    if (!docsFolder.exists()) {
       docsFolder.mkdir();
    }
    File file = new File(docsFolder.getAbsolutePath(),"test.txt"); 
    

    编辑:

    public static String getExternalSdCardPath() {
        String path = null;
    
        File sdCardFile = null;
        List<String> sdCardPossiblePath = Arrays.asList("external_sd", "ext_sd", "external", "extSdCard");
    
        for (String sdPath : sdCardPossiblePath) {
                File file = new File("/mnt/", sdPath);
    
                if (file.isDirectory() && file.canWrite()) {
                        path = file.getAbsolutePath();
    
                        String timeStamp = new SimpleDateFormat("ddMMyyyy_HHmmss").format(new Date());
                        File testWritable = new File(path, "test_" + timeStamp);
    
                        if (testWritable.mkdirs()) {
                                testWritable.delete();
                        }
                        else {
                                path = null;
                        }
                }
        }
    
        if (path != null) {
                sdCardFile = new File(path);
        }
        else {
                sdCardFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
        }
    
        return sdCardFile.getAbsolutePath();
    

    }

    资源:

    https://gist.github.com/PauloLuan/4bcecc086095bce28e22 https://www.codeproject.com/Questions/716256/find-path-of-external-SD-card

    【讨论】:

    • 没有。 getExternalStorageDirectory() 将给出外部存储器的路径。不是可移动的微型 SD 卡。所以你用粗体标出的漂亮的路径你永远不会得到。
    • 但它不是 SD 卡(可移动)。我测试了这段代码。 Environment.getExternalStorageDirectory() 返回内部存储
    • 很糟糕,我没有看到删除部分,我添加了一些可能有用的资源。谢谢
    猜你喜欢
    • 2020-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多