android - 在内部和外部 SdCard 上存储文件

标签 android android-sdcard

我正在尝试在内部和外部 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/

相关文章:

android - android中具有不同单元格宽度的RecyclerView Grid的LayoutManager

Android为多个模拟器重用SD卡

android - 滚动网格时在 gridview 的情况下重复图像?

Android:将评分栏的颜色更改为金色

android - 如何使用 Robotium 单击微调器中的第一项?

Android - 当应用程序安装在 SD 卡上时,内部存储与外部存储

android - 如何访问 webview 中包含的 html 元素?

android - 如何检查外部存储空间的可用性?

java - invalidate() 在空对象引用上

java - 安卓java : Anonymous sign in with Firebase always fails