[英]file created in sd card missing?
我正在使用此代碼在SD卡上創建和寫入文件,但是在運行我的Android應用后,我似乎找不到在SD卡中創建的任何文件。 我不確定我的代碼是否有問題或什么。 請幫我。 任何意見將不勝感激..謝謝!
這是我正在使用的代碼:
private void writeToSDFile() {
File root = android.os.Environment.getExternalStorageDirectory();
tv.append("\nExternal file system root: "+root);
File dir = new File (root.getAbsolutePath());
dir.mkdirs();
File file = new File(dir, "wordlist.txt");
try {
FileOutputStream f = new FileOutputStream(file);
PrintWriter pw = new PrintWriter(f);
pw.println(stringword);
pw.append(stringword);
pw.flush();
pw.close();
f.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
Log.i(TAG, "******* File not found.);
} catch (IOException e) {
e.printStackTrace();
}
tv.append("\n\nFile written to "+file);
}//end writeToSDFile
您的代碼中似乎沒有什么錯,您必須確保兩件事:
1-您在清單中具有適當的權限,即WRITE_EXTERNAL_STORAGE
2- mounted
外部存儲器
if(Environment.getExternalStorageState() == Environment.MEDIA_MOUNTED){
// write your file
}
您的代碼中有冗余,行File dir = new File (root.getAbsolutePath());
絕對沒用。
您需要的是:
File file = new File(Environment.getExternalStorageDirectory(), "wordlist.txt");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.