Как записать файл на карту памяти?

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();
        }
    }

Помогите пожалуйста разобраться. Кстати на эмуляторе работает.
  • Вопрос задан
  • 2733 просмотра
Пригласить эксперта
Ответы на вопрос 3
@bimeg
Может не хватает пермишона?

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Ответ написан
Collosteam
@Collosteam
Android кодирую
Вот рабочий тест

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);


    }
Ответ написан
@kirawa Автор вопроса
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){
         //не заходит  в блок   
        }
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы