0

Im having a problem saving/ creating a file on my sdcard to save text to, i am trying to check if the file is created and if not create it, then write certain data to it on button click. Here is my code and error output:

This is the error output:

java.lang.RuntimeException: Unable to start activity ComponentInfo{ibettergetagoodgradeforthisorillbepissed.sciencefair.beta.mmmeds.com.mmmeds/ibettergetagoodgradeforthisorillbepissed.sciencefair.beta.mmmeds.com.mmmeds.MainActivity}: java.lang.IllegalArgumentException: File /sdcard/output.txt contains a path separator
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2187)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2236)
            at android.app.ActivityThread.access$800(ActivityThread.java:138)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1199)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5034)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1270)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1086)
            at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.IllegalArgumentException: File /sdcard/output.txt contains a path separator
            at android.app.ContextImpl.makeFilename(ContextImpl.java:2165)
            at android.app.ContextImpl.getFileStreamPath(ContextImpl.java:964)
            at android.content.ContextWrapper.getFileStreamPath(ContextWrapper.java:195)
            at ibettergetagoodgradeforthisorillbepissed.sciencefair.beta.mmmeds.com.mmmeds.MainActivity.onCreate(MainActivity.java:207)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2151)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2236)
            at android.app.ActivityThread.access$800(ActivityThread.java:138)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1199)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5034)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)

and here is the code for the file saving:

String SDRoot = Environment.getExternalStorageDirectory().getPath();
            final File output = getFileStreamPath(SDRoot + "/output.txt");
        if(!output.exists()){
            try {
                output.createNewFile();
                Context context = getApplicationContext();
                CharSequence text = "Output File Created!";
                int duration = Toast.LENGTH_LONG;

                Toast fileCreated = Toast.makeText(context, text, duration);
                fileCreated.show();
            } catch (IOException e) {
                Context context = getApplicationContext();
                CharSequence text = "Output File Not Created!";
                int duration = Toast.LENGTH_LONG;

                Toast fileNotCreated = Toast.makeText(context, text, duration);
                fileNotCreated.show();
                e.printStackTrace();
            }
        }


            Button addbutton = (Button)findViewById(R.id.addMeds);
        addbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String outputLine = medOut.toString() + doseOut.getText() + dayout.getText() + timeOut.getText();

                try {
                    FileOutputStream fOut = new FileOutputStream(output);
                    OutputStreamWriter myOutputStreamWriter = new OutputStreamWriter(fOut);
                    myOutputStreamWriter.append(outputLine);
                    myOutputStreamWriter.flush();
                    myOutputStreamWriter.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                    Toast.makeText(getBaseContext(), e.getMessage(),
                            Toast.LENGTH_SHORT).show();
                } catch (IOException e) {
                    e.printStackTrace();
                    Toast.makeText(getBaseContext(), e.getMessage(),
                            Toast.LENGTH_SHORT).show();
                }

            }
        });
}
Michael Garber
  • 2,896
  • 2
  • 11
  • 14

1 Answers1

0

http://developer.android.com/reference/android/content/Context.html#getFileStreamPath(java.lang.String)

Parameters : name The name of the file for which you would like to get its path.

If you give a path (containing "/") instead, it will crash as you have already noticed.

Besides, this function applies only for your application private files, created with openFileOuput for instance.

http://developer.android.com/reference/android/content/Context.html#openFileOutput(java.lang.String, int)

Just do like this :

String SDRoot = Environment.getExternalStorageDirectory().getPath();
final File output = new File(SDRoot + "/output.txt");
ToYonos
  • 16,469
  • 2
  • 54
  • 70