0

I want to create a simple textfile on an Android device on external SD card. With my code, i always get the error: java.io.FileNotFoundException: /storage/emulated/0/SFX/TEST1.txt: open failed: ENOENT (No such file or directory)

i have set <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> in my manifest file

        Button btn = (Button)findViewById(R.id.btn_Save);
        btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //mach was
            StringBuilder sb = new StringBuilder();
            sb.append(txtName.getText().toString());
            sb.append(System.getProperty("line.separator"));
            sb.append(txtMonteur.getText().toString());
            sb.append(System.getProperty("line.separator"));
            sb.append(txtStraße.getText().toString());
            sb.append(System.getProperty("line.separator"));
            sb.append(Energiearten);

            File file;
            FileOutputStream out;
            try
            {
                file = new File(Environment.getExternalStorageDirectory(), "SFX/TEST1.txt");
                file.mkdirs();
                out = new FileOutputStream(file);
                out.write(sb.toString().getBytes());
                out.close();

            } catch (IOException e) {
                e.printStackTrace();

            } catch (Exception e) {
                Toast.makeText(ZBewegung.this, "Error:" + e.toString(), Toast.LENGTH_LONG).show();
            }
        }
    });
Sep Pei
  • 21
  • 2
  • 2
    If you are testing in >= api 23 you have to write code for check permission also. if not then post your error log also here. – Wasim K. Memon Jan 03 '17 at 14:13
  • 1
    [Implement runtime permissions](https://stackoverflow.com/questions/32635704/android-permission-doesnt-work-even-if-i-have-declared-it). Also note that your code does not pertain to an SD card, as [external storage](https://commonsware.com/blog/2014/04/08/storage-situation-external-storage.html) is not [removable storage](https://commonsware.com/blog/2014/04/09/storage-situation-removable-storage.html) on most Android devices. – CommonsWare Jan 03 '17 at 14:14
  • check SD card for whether the file is created or not? – Rajesh.k Jan 03 '17 at 14:29

1 Answers1

0
file = new File(Environment.getExternalStorageDirectory(), "SFX/TEST1.txt");
file.mkdirs();

This creates a directory named SFX/TEST1.txt, which is not what you want.

Try:

File dir=new File(Environment.getExternalStorageDirectory(), "SFX");
dir.mkdirs();
file = new File(dir, "SFX/TEST1.txt");
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • with your code and by implementing this (http://stackoverflow.com/questions/33666071/android-marshmallow-request-permission) i can successfully write to the internal storage (not the removable SD card). Any suggestions how to write to this to the sd card? doesnt getExternalStorageDirectory() mean sd card? – Sep Pei Jan 04 '17 at 10:14
  • @SepPei: "doesnt getExternalStorageDirectory() mean sd card?" -- no. As I noted in a comment on your question, [external storage](https://commonsware.com/blog/2014/04/08/storage-situation-external-storage.html) is not [removable storage](https://commonsware.com/blog/2014/04/09/storage-situation-removable-storage.html). On Android 4.4+, use `getExternalFilesDirs()`, and look for the second or subsequent `File` objects in the returned list. Those should be on removable storage, pointing to directories in which your app can read and write. – CommonsWare Jan 04 '17 at 12:15