K
K
kirawa2014-05-23 08:04:58
Android
kirawa, 2014-05-23 08:04:58

How to write a file to a memory card?

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

Please help me figure it out. By the way, it works on the emulator.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
B
bimeg, 2014-05-23
@bimeg

Maybe not enough permishon?

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

A
Andrey M, 2014-05-23
@Collosteam

Here is a working test

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


    }

K
kirawa, 2014-05-23
@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){
         //не заходит  в блок   
        }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question