我正在尝试在内部和外部 SD 卡上保存文件..基本上我想给用户一个选项,将文件保存在内部或外部卡上 这就是我在内部 SD 卡中创建文件的方式
File file = new File(Environment.getExternalStorageDirectory()
+ "/Tnt_input/register.html");
但是如何在外部 SD 卡
中创建这个文件...?
我可以在DDMS中看到两个存储
1) sdCard0
2) extSdCard
通过使用上面的代码在 sdCard0
中创建这个文件
最佳答案
您可以使用context.getFilesDir()
将文件保存到内部存储
File file = new File(context.getFilesDir(), filename);
String filename = "myfile";
String string = "Hello world!";
FileOutputStream outputStream;
try {
outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
outputStream.write(string.getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
检查 saving a file to internal storage获取更多信息。
注意:您的应用程序的内部存储目录由您的应用程序包名称指定,位于 Android 文件系统的一个特殊位置。从技术上讲,如果您将文件模式设置为可读,则另一个应用可以读取您的内部文件。
Good Example for Save and read file from/to Internal/External storage
how to differentiate between internal and external SD card path in android?
关于android - 在内部和外部 SdCard 上存储文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23466753/