我知道有这方面的问题,但由于某种原因,似乎没有任何工作适合我。 我试图从我的应用程序中获取2个文本文件以保存到SD卡。它可以正确地创建目录和文件,但始终是内部存储,永远不会是外部存储。我在Manifest中也有权限。Android将文件保存到SD卡,而不是内部存储
try {
File sdCard = Environment.getExternalStorageDirectory();
File myFile = new File(sdCard.getAbsolutePath() + "/rlgl");
myFile.mkdir();
// myFile.createNewFile();
String newLine = System.getProperty("line.separator");
File file = new File(myFile, "rlgls.txt");
if(file.exists()) {
} else if (!file.exists()){
FileOutputStream fOut = new FileOutputStream(file);
OutputStreamWriter myOutWriter =
new OutputStreamWriter(fOut);
for (int i = 0; i < 30; i++) {
myOutWriter.append("0.0" + newLine);
}
myOutWriter.close();
fOut.close();
}
} catch (IOException e) {
e.printStackTrace();
}
这是我正在使用的代码。我遵循其他Stackoverflow响应的指示,但它永远不会去SD卡。有什么我做错了吗?另外一个后续问题是否有一种方法可以让我使用上述代码来使文件对用户不可见。他们应该没有理由打开它们。提前致谢。
Android将保存到默认存储。您必须在设备系统设置中手动设置默认存储 – 2014-08-27 15:52:44
Android API实际上并不能让您获取设备的SD卡。 [getExternalStorageDirectory](https://developer.android.com/reference/android/os/Environment.html#getExternalStorageDirectory())方法将返回默认的共享存储,这可能是也可能不是SD卡。 – tcooc 2014-08-27 16:01:03
感谢您的信息。我还有很多东西需要学习。我还有一个问题。我知道把一个“。”在文件或目录名称前面使其不可见,但这是否也意味着应用程序无法读取它?我正在试验这个,当有一个“。”时,我的应用程序无法从文件读取/写入文件。在他们的名字前面。 – user2835796 2014-08-27 16:28:11