无法将文件保存到SDcard Android应用程序

时间:2014-10-18 22:38:42

标签: java android file

我在sdcard上保存/创建文件以保存文本时遇到问题,我正在尝试检查文件是否已创建,如果没有创建,则在按钮点击时将某些数据写入其中。这是我的代码和错误输出:

这是错误输出:

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)

以下是文件保存的代码:

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

            }
        });
}

1 个答案:

答案 0 :(得分:0)

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

  

参数:name要获取其路径的文件的名称。

如果你提供一个路径(包含" /"),它会像你已经注意到的那样崩溃。

此外,此功能仅适用于使用openFileOuput创建的应用程序专用文件。

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

就这样做:

String SDRoot = Environment.getExternalStorageDirectory().getPath();
final File output = new File(SDRoot + "/output.txt");