void writeFileSD(Context context) {
File sdFile = null;
if (!Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
Toast.makeText(context,"SD-карта не обнаружена",Toast.LENGTH_SHORT).show();
return;
}
File sdPath = Environment.getExternalStorageDirectory();
sdPath = new File(sdPath.getAbsolutePath() + "/" + DIR_SD);
sdPath.mkdirs();
if (!sdPath.exists()){
sdFile = new File(sdPath, FILENAME_SD);}
try {
if (sdFile != null){
BufferedWriter bw = new BufferedWriter(new FileWriter(sdFile));//программа умирает здесь с ошибкой: java.io.FileNotFoundException: /mnt/sdcard/Note/export.txt: open failed: ENOENT (No such file or directory)
if (notes.size() == 0){
Toast.makeText(context,"Заметки не найдены",Toast.LENGTH_SHORT).show();
}else {
for (Note note : notes) {
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm");
long l = note.getData();
String times = sdf.format(new Date(l));
bw.write(times+"\n" +note.getName() + "\r\n"+"\r\n");
}
Toast.makeText(context, "Файл записан на SD: " + sdFile.getAbsolutePath(),Toast.LENGTH_LONG).show();
}
bw.close();}
} catch (IOException e) {
e.printStackTrace();
}
}
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
public void testCreateFile() throws IOException {
String path = File.separator + "tmp_test";
File DIR = null;
boolean b = false;
DIR = new File(Environment.getExternalStorageDirectory(), path);
b = DIR.mkdirs();
File file = new File(DIR, File.separator + "file.ok");
file.createNewFile();
String string = "Hello world!";
FileOutputStream outputStream;
try {
outputStream = activity.openFileOutput(file.getAbsolutePath(),Context.MODE_PRIVATE);
outputStream.write(string.getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
File fileRes = new File(file.getAbsolutePath());
assertNotNull(file);
assertEquals(file.exists(),true);
}
String path = Environment.getExternalStorageDirectory().toString()+File.separator+DIR_SD+File.separator+FILENAME_SD;
File file = null;
boolean b = false;
file = new File(path);
b = file.mkdirs();
try {
b = file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
if (b){
//не заходит в блок
}