[英]Save file to Specific folder on SD card (External Storage Removable)
我尝试使用context.getExternalFileDirs()
路径进行保存,但是它将文件保存到/storage/9016-4EF8/Android/data/package/files
文件夹/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
。 我该如何实现?
如果要写入整个micro SD卡,请使用Storage Access Framework。
使用Intent.ACTION_OPEN_DOCUMENT_TREE
让您的应用用户选择卡片。
在Android 7+上,请查看存储卷。
这里的链接说明了如何让用户选择内部或外部存储。 您让用户选择路径并维护该静态变量,然后所有数据将使用THE_PATH变量写入SQLite DB。
您可以使用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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.