我正在尝试在我的应用中导出一些数据。即使我多次在清单和已删除的构建上设置了所需的权限,它也会产生相同的错误。我该如何解决? FileNotFoundException:/storage/emulated/0/VocAppFile/output.txt(没有这样的文件或目录) 事情是我的手机没有上面这样的文件夹。但/ storage / emulated / 0 /是默认目录。
我的代码是:
String state = Environment.getExternalStorageState();
if(Environment.MEDIA_MOUNTED.equals(state)){
File Root = Environment.getExternalStorageDirectory();
Log.d("Export to", Root.getAbsolutePath());
File Dir = new File(Root.getAbsolutePath()+"/AppFile");
if(!Dir.exists()){
Dir.mkdir();
}
File file = new File(Dir,"output.txt");
try {
FileOutputStream fos = new FileOutputStream(file);
for(String[] d : data){
//data comes ready
fos.write(d[0].getBytes());
fos.write(d[1].getBytes());
}
fos.close();
Toast.makeText(getApplicationContext(), "Data exported",
Toast.LENGTH_SHORT).show();
}catch (FileNotFoundException e){
Log.wtf("My activity", "Error in exporting words");
e.printStackTrace();
}catch (IOException e){
Log.wtf("My activity", "Error in exporting words");
e.printStackTrace();
}
答案 0 :(得分:0)
在File Dir = ...
新行
file.createNewFile();
最终守则将是这样的
String state = Environment.getExternalStorageState();
if(Environment.MEDIA_MOUNTED.equals(state)){
File Root = Environment.getExternalStorageDirectory();
Log.d("Export to", Root.getAbsolutePath());
File Dir = new File(Root.getAbsolutePath()+"/AppFile");
if(!Dir.exists()){
Dir.mkdir();
}
File file = new File(Dir,"output.txt");
file.createNewFile();
try {
FileOutputStream fos = new FileOutputStream(file);
for(String[] d : data){
//data comes ready
fos.write(d[0].getBytes());
fos.write(d[1].getBytes());
}
fos.close();
Toast.makeText(getApplicationContext(), "Data exported",
Toast.LENGTH_SHORT).show();
}catch (FileNotFoundException e){
Log.wtf("My activity", "Error in exporting words");
e.printStackTrace();
}catch (IOException e){
Log.wtf("My activity", "Error in exporting words");
e.printStackTrace();
}
答案 1 :(得分:0)