[英]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.