给出以下代码:
final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS) + "/";
String file = dir + "info.txt";
File newfile = new File(file);
//Environment.getExternalStorageDirectory().toString()= /storage/emulated/0
String msgData="p1000";
FileOutputStream outputStream;
try {
newfile.createNewFile();
outputStream = openFileOutput(file, Context.MODE_PRIVATE);
outputStream.write(msgData.getBytes());
outputStream.close();
} catch (Exception e) {
e.toString();
//e.printStackTrace();
}
当我打开文件/storage/emulated/0/Documents/info.text时,我发现它为空,而它应该具有字符串“ p1000”;
这是为什么?
注意:我没有外部存储(外部SD卡)。
谢谢。
最佳答案
您的清单中是否具有WRITE_EXTERNAL_STORAGE权限?
试试这个:FileOutputStream outputStream = new FileOutputStream(file);