Java FileNotFoundException-创建文件并保存到外部存储时。-安卓
我正在尝试将文件保存到外部存储器,但是它给出了FileNotFoundException错误 注意:我已经对Android清单进行了更改Java FileNotFoundException-创建文件并保存到外部存储时。-安卓,java,android,file,android-fragments,android-activity,Java,Android,File,Android Fragments,Android Activity,我正在尝试将文件保存到外部存储器,但是它给出了FileNotFoundException错误 注意:我已经对Android清单进行了更改 File file = null; if (isExternalStorageWritable()) { Log.d("SEND", "Entrou no IF do ExernalStorage"); //File path = Environment.getExternalStorag
File file = null;
if (isExternalStorageWritable()) {
Log.d("SEND", "Entrou no IF do ExernalStorage");
//File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File path = ContaListFragment.this.getActivity()
.getApplicationContext()
.getExternalFilesDir(
Environment.DIRECTORY_PICTURES);//getExternalFilesDir
file = new File(path, "file.json");
//Make sure the Pictures directory exists.
Log.d("SEND", "" + path.mkdir() + " PATH=" + file.getAbsolutePath());
//file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), "file.json");
}
if (!file.mkdirs()) {
Log.d("SEND", "Directory not created");
}
if (!isExternalStorageWritable()) {
file = new File(ContaListFragment.this.getActivity()
.getApplicationContext().getFilesDir(), "file.json");
Log.d("SEND", "Passei do File");
output = new BufferedWriter(new FileWriter(file));
Log.d("SEND", "Passou do Buffered");
output.write(jsonObject.toString());
uri = Uri.fromFile(file);
}
在以下人员的帮助下,问题得以解决: 问题是我创建的是一个目录而不是一个文件。只需替换:file.mkdirs();使用file.createNewFile()