我想創建一個.txt文件並將其存儲在Android手機的外部存儲器中。我向Android Manifest添加了權限。當我運行代碼時,它不會給我任何錯誤,但是文件永遠不會被創建。不知道我做錯了什麼。無法在外部存儲器上創建文件Android
public void createExternalStoragePrivateFile(String data) {
// Create a path where we will place our private file on external
// storage.
File file = new File(myContext.getExternalFilesDir(null), "state.txt");
try {
FileOutputStream os = null;
OutputStreamWriter out = null;
os = myContext.openFileOutput(data, Context.MODE_PRIVATE);
out = new OutputStreamWriter(os);
out.write(data);
os.close();
if(hasExternalStoragePrivateFile()) {
Log.w("ExternalStorageFileCreation", "File Created");
} else {
Log.w("ExternalStorageFileCreation", "File Not Created");
}
} catch (IOException e) {
// Unable to create file, likely because external storage is
// not currently mounted.
Log.w("ExternalStorage", "Error writing " + file, e);
}
}
這會在'ExtSdCard/Android/data/packagename/files'中創建一個空文件夾。但是在'DeviceStorage/Android/data/packagename/files'中創建'state.txt'文本文件。那麼如何在外部SD卡文件夾中創建'state.txt'? – 2017-03-24 16:21:21