繁体   English   中英

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

[英]Cannot save files to SDcard android app

我在保存/在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();
                }

            }
        });
}

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");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM