Android 将文件保存到SD卡上的特定文件夹(外部存储器可移动)

Android 将文件保存到SD卡上的特定文件夹(外部存储器可移动),android,Android,我试图使用pathcontext.getExternalFileDirs()保存文件,但它将文件保存到SD卡上的文件夹/storage/9016-4EF8/Android/data/package/files File file = new File(context.getExternalFilesDir(null), myFolder); 我搜索到Android只支持对应用程序的文件夹进行读/写操作,如/Android/data/package/files,但我想将文件保存到特定文件夹,如/

我试图使用path
context.getExternalFileDirs()
保存文件,但它将文件保存到SD卡上的文件夹
/storage/9016-4EF8/Android/data/package/files

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

我搜索到Android只支持对应用程序的文件夹进行读/写操作,如
/Android/data/package/files
,但我想将文件保存到特定文件夹,如
/storage/9016-4EF8/MyFolder
。我怎样才能做到这一点

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

使用
Intent.ACTION\u OPEN\u DOCUMENT\u TREE
让应用程序的用户选择卡


在Android 7+上,请查看存储卷。

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

使用
Intent.ACTION\u OPEN\u DOCUMENT\u TREE
让应用程序的用户选择卡


在Android 7+上查看存储卷。

您可以使用
环境。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();
public静态字符串getExternalSdCardPath(){
字符串路径=null;
文件sdCardFile=null;
List-sdCardPossiblePath=Arrays.asList(“外部”、“外部”、“外部”、“外部sd卡”);
用于(字符串sdPath:sdCardPossiblePath){
File File=新文件(“/mnt/”,sdPath);
if(file.isDirectory()&&file.canWrite()){
path=file.getAbsolutePath();
字符串时间戳=新的SimpleDateFormat(“ddMMyyyy_HHmmss”)。格式(新日期();
文件testWritable=新文件(路径“test_u2;”+时间戳);
if(testWritable.mkdirs()){
testWritable.delete();
}
否则{
path=null;
}
}
}
if(路径!=null){
sdCardFile=新文件(路径);
}
否则{
sdCardFile=新文件(Environment.getExternalStorageDirectory().getAbsolutePath());
}
返回sdCardFile.getAbsolutePath();
}

资源:


您可以使用
环境。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();
public静态字符串getExternalSdCardPath(){
字符串路径=null;
文件sdCardFile=null;
List-sdCardPossiblePath=Arrays.asList(“外部”、“外部”、“外部”、“外部sd卡”);
用于(字符串sdPath:sdCardPossiblePath){
File File=新文件(“/mnt/”,sdPath);
if(file.isDirectory()&&file.canWrite()){
path=file.getAbsolutePath();
字符串时间戳=新的SimpleDateFormat(“ddMMyyyy_HHmmss”)。格式(新日期();
文件testWritable=新文件(路径“test_u2;”+时间戳);
if(testWritable.mkdirs()){
testWritable.delete();
}
否则{
path=null;
}
}
}
if(路径!=null){
sdCardFile=新文件(路径);
}
否则{
sdCardFile=新文件(Environment.getExternalStorageDirectory().getAbsolutePath());
}
返回sdCardFile.getAbsolutePath();
}

资源:


这里有一个指向的链接,说明如何让用户选择内部或外部存储。您可以让用户选择路径并维护该静态变量,所有数据都将使用_path变量写入SQLite DB


这里有一个指向的链接,说明如何让用户选择内部或外部存储。您可以让用户选择路径并维护该静态变量,所有数据都将使用_path变量写入SQLite DB


您可以添加用于此目的的代码吗?这已经是sd卡上特定于应用程序的文件夹。所以这应该是可写的。sd卡的其余部分无法使用文件系统写入。我需要一个解决方案将数据保存到特定文件夹。因为这样的话,/Android/Data/package/files上的数据会有问题,所以当用户更新应用程序或卸载/重新安装时,它会被删除。是的,我知道。没有更新。仅在卸载时。现在阅读我的评论并回答,这样你们就知道你们要做什么了。嗨@greenapps我不确定case用户更新应用程序。你确定应用程序数据不会随更新而删除吗?你能添加用于此操作的代码吗?这已经是sd卡上特定于应用程序的文件夹。所以这应该是可写的。sd卡的其余部分无法使用文件系统写入。我需要一个解决方案将数据保存到特定文件夹。因为这样的话,/Android/Data/package/files上的数据会有问题,所以当用户更新应用程序或卸载/重新安装时,它会被删除。是的,我知道。没有更新。仅在卸载时。现在阅读我的评论并回答,这样你们就知道你们要做什么了。嗨@greenapps我不确定case用户更新应用程序。你确定应用程序数据不会随着更新而删除吗?如果你有一个关于使用Storage Access Framework写入数据的示例,那就很好了,只需谷歌一下。有这么多的例子使用这个意图。还有Intent.ACTION\u OPEN\u文档和Intent.ACTION\u CREATE\u文档。全部试试。@YoungKing发布了对您的问题的回答,发表了您想要更多帮助的评论。完整代码可以在我的网站上通过教程androidstackoverflow.com进行解释。如果您有一个关于使用Storage Access Framework编写数据的示例,请用谷歌搜索一下。有这么多的例子使用这个意图。还有Intent.ACTION\u OPEN\u文档和Intent.ACTION\u CREATE\u文档。“都试试看。”杨金把答案贴到了y上