Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android not creating file

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)
like image 461
user3033922 Avatar asked Nov 25 '13 20:11

user3033922


2 Answers

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();
}
like image 113
Brianjs Avatar answered Oct 21 '22 08:10

Brianjs


Add this permissions in manifest.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
like image 9
anjaneya Avatar answered Oct 21 '22 09:10

anjaneya