2014-10-18 90 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");