2014-10-18 184 views
0

我有一个问题在我的SD卡上保存/创建一个文件来保存文本,我试图检查文件是否创建,如果不创建它,然后写入某些数据按钮点击。这里是我的代码和错误输出:无法保存文件到SD卡android应用程序

这是错误输出:

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

      } 
     }); 
} 
+0

我无法找到这个问题@Omersonmez – 2014-10-18 22:47:19

+1

你需要使用的文件名与出路径中我的答案。看起来你的文件名包含一个路径sdcard。 – 2014-10-18 22:53:16

+0

那么我如何确定文件的创建地点? @mersonmez – 2014-10-18 22:55:18

回答

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