即使外部存储在 android 中可用,如何将文件(公开)保存在内部存储上?

How to save files(publically) on internal storage even if the external storage is available in android?

即使外部存储在 android 中可用,如何将文件(公开)保存在内部存储上?

即使挂载了外部存储,我也想将文件保存在内部存储根路径上。

假设有一个文本文件并保存到本地存储

public boolean saveFile(Context context, String myTextString){
    try {
        FileOutputStream fos = context.openFileOutput(getFilename(),Context.MODE_PRIVATE);
        Writer out = new OutputStreamWriter(fos);
        out.write(myTextString);
        out.close();
        return true;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
}

private String getFilename() {
    String filepath = Environment.getExternalStorageDirectory().getPath();
    File file = new File(filepath);
    if (!file.exists()) {
        file.mkdirs();
    }
    return (file.getAbsolutePath() + "/" + System.currentTimeMillis() + ".txt");

}

希望这对您有所帮助..