在外部存储的Android无法创建文件 [英] Impossible to create file on external storage Android
本文介绍了在外部存储的Android无法创建文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想创建一个文本文件,并将其存储在Android手机上的外部存储。我添加的权限,我的Android清单。当我运行code它并没有给我任何错误,但该文件是永远不会创建。不知道我做错了。
公共无效createExternalStoragePrivateFile(字符串数据){
//创建一个路径,我们将放置在外部我们的私人文件
// 存储。
档案文件=新的文件(myContext.getExternalFilesDir(空),state.txt);
尝试 {
FileOutputStream中OS = NULL;
OutputStreamWriter了= NULL;
OS = myContext.openFileOutput(数据,Context.MODE_PRIVATE);
OUT =新OutputStreamWriter(OS);
out.write(数据);
os.close();
如果(hasExternalStoragePrivateFile()){
Log.w(ExternalStorageFileCreation,文件创建);
} 其他 {
Log.w(ExternalStorageFileCreation,文件未创建);
}
}赶上(IOException异常E){
//无法创建文件,很可能是因为外部存储
//当前未安装。
Log.w(ExternalStorage,写入+文件,E);
}
}
解决方案
档案文件=新的文件(myContext.getExternalFilesDir(空),state.txt);
尝试 {
FileOutputStream中OS =新的FileOutputStream(文件,真正的);
OutputStreamWriter OUT =新OutputStreamWriter(OS);
out.write(数据);
out.close();
}
I want to create a .txt file and store it on the external storage of the Android phone. I added the permission to my Android Manifest. When I run the code it doesn't give me any error but the file is never created. Not sure what I am doing wrong.
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);
}
}
解决方案
File file = new File(myContext.getExternalFilesDir(null), "state.txt");
try {
FileOutputStream os = new FileOutputStream(file, true);
OutputStreamWriter out = new OutputStreamWriter(os);
out.write(data);
out.close();
}
这篇关于在外部存储的Android无法创建文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文