我尝试使用路径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
。我怎样才能做到这一点?
答案 0 :(得分:0)
如果要写入整个micro SD卡,请使用Storage Access Framework。
使用Intent.ACTION_OPEN_DOCUMENT_TREE
让您应用的用户选择该卡。
在Android 7+上看看Storage Volumes。
答案 1 :(得分:0)
您可以使用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