android上面,会遇到no such file or dir

来源:互联网 发布:腾讯视频会员破解软件 编辑:程序博客网 时间:2024/06/11 23:32

android上面,会遇到no such file or dir.

有些手机会这样,不知道什么原因,所以在这边做一下笔记,只要加上下面红色的代码就可以



public void saveFile(String folder, String str, String filename) {
FileOutputStream fileOutputStream = null;
File file;
try {
file = new File(Environment.getExternalStorageDirectory()
       + File.separator +folder,filename);// new String(filename.trim().getBytes(),"utf-8"));
if (!file.getParentFile().exists()) {
file.getParentFile().mkdir();
}

fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(str.getBytes());
fileOutputStream.close();
} catch (Exception  e) {
e.printStackTrace();
}
}