10

I want this fragment to write to a csv file when a button is clicked but I keep getting java.io.IOException: open failed:ENOENT (No such file or directory). Any help would be greatly appreciated.

public class AddFragment extends Fragment {

    static EditText spent,saved,coupons;
    Button writeExcelButton;
    String data;
    Spinner spinner;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.add_layout, container, false);

        setSpinnerContent(view);

        spent = (EditText) view.findViewById(R.id.spent1);
        saved = (EditText) view.findViewById(R.id.saved1);
        coupons = (EditText) view.findViewById(R.id.coupons1);

        writeExcelButton = (Button) view.findViewById(R.id.button_addGroc);
        writeExcelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              updateSheet();
            }
        });

        return view;
    }

    private void setSpinnerContent (View view) {
        spinner = (Spinner) view.findViewById(R.id.groc_store);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(),
            R.array.store1, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
    }

    public void updateSheet() {
        try {
            // This is the string that should be written to file
            String mySpin =spinner.getSelectedItem().toString();
            data = mySpin + "," + spent.getText().toString() + "," + saved.getText().toString() + "," + coupons.getText().toString() + "/n";

            // This is the file that should be written to
            String sdCard = Environment.getExternalStorageDirectory().toString();
            File dir = new File(sdCard + "/dir");
            if (!dir.exists()) {
                dir.mkdir();
            }

            File myFile = new File(dir.getAbsolutePath(), "savings.csv");

            // if file doesn't exists, then create it
            if (!myFile.exists()) {
                myFile.createNewFile();
            }

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

This is what my LogCat looks like

11-14 13:29:23.681: W/System.err(14386): java.io.IOException: open failed: ENOENT (No such file or directory)
11-14 13:29:23.681: W/System.err(14386):    at java.io.File.createNewFile(File.java:948)
11-14 13:29:23.691: W/System.err(14386):    at com.example.myfirstapp.AddFragment.updateSheet(AddFragment.java:101)
11-14 13:29:23.691: W/System.err(14386):    at com.example.myfirstapp.AddFragment$1.onClick(AddFragment.java:59)
11-14 13:29:23.691: W/System.err(14386):    at android.view.View.performClick(View.java:4240)
11-14 13:29:23.691: W/System.err(14386):    at android.view.View$PerformClick.run(View.java:17721)
11-14 13:29:23.691: W/System.err(14386):    at android.os.Handler.handleCallback(Handler.java:730)
11-14 13:29:23.691: W/System.err(14386):    at android.os.Handler.dispatchMessage(Handler.java:92)
11-14 13:29:23.691: W/System.err(14386):    at android.os.Looper.loop(Looper.java:137)
11-14 13:29:23.691: W/System.err(14386):    at android.app.ActivityThread.main(ActivityThread.java:5103)
11-14 13:29:23.691: W/System.err(14386):    at java.lang.reflect.Method.invokeNative(Native Method)
11-14 13:29:23.691: W/System.err(14386):    at java.lang.reflect.Method.invoke(Method.java:525)
11-14 13:29:23.701: W/System.err(14386):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-14 13:29:23.711: W/System.err(14386):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-14 13:29:23.711: W/System.err(14386):    at dalvik.system.NativeStart.main(Native Method)
11-14 13:29:23.711: W/System.err(14386): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
11-14 13:29:23.711: W/System.err(14386):    at libcore.io.Posix.open(Native Method)
11-14 13:29:23.711: W/System.err(14386):    at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
11-14 13:29:23.711: W/System.err(14386):    at java.io.File.createNewFile(File.java:941)
1
  • do you have <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> in your manifest ? Commented Nov 25, 2013 at 20:58

2 Answers 2

23

Try changing

dir.mkdir();

to

dir.mkdirs();

Also try just passing dir instead of dir.getAbsolutePath()

Edit

Also you don't want to concatanate file paths like that. Try:

File myFile = new File(Environment.getExternalStorageDirectory(), "dir/savings.csv");
if (!myFile.exists()) {
     myFile.mkdirs();
     myFile.createNewFile();
}
Sign up to request clarification or add additional context in comments.

6 Comments

I tried both of your suggestions with no luck. I'm trying to run this on an emulator, if that helps.
Have you added SDCard support to the emulator? Go to eclipse - windows - avd manager - select avd and edit - hardware - new - SDCard Support
Alright so I had added an SDCard to the AVD after I had already started the AVD and I didn't think to restart it... Right now nothing shows up in LogCat when I hit the button but I also can't find the file in File Explorer. There doesn't seem to be another directory after mnt/sdcard.
The file still isn't showing up. Do I need to write something to the file?
Ok I found the file. It was in storage/sdcard/dir... Sorry, I'm new to this. Every resource I looked up said it should be in mnt/sdcard. Thank you Brian for your help and time.
|
9

Add this permissions in manifest.

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

1 Comment

Indeed this a very good catch!! If user is not adding this permission, the android system will not allow user to write and the same ENOENT error will occur.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.