2012-05-18 99 views
0

我想要保存文件到我的SD卡。這是我迄今爲止的代碼。我非常感謝您提供的任何幫助。謝謝 編輯: 問題是該文件未保存。當我退出程序並查看SD卡時,沒有文件被保存。我向錯誤捕獲器中添加了一個敬酒,它看起來像我得到一個IOException。我不知道如何檢查堆棧跟蹤日誌雖然在android中保存文件到SD卡的問題

class bSaveListen implements Button.OnClickListener{ 
    @Override 

    public void onClick(View arg0) { 
     savef(); 
    } 

} 

public void savef(){ 
    sdCard = Environment.getExternalStorageDirectory(); 
    String filename = "aaaaaaa.txt"; 
    file = new File(sdCard, filename); 
    FileOutputStream fileout; 
    byte[] thatString = new String("awesome string").getBytes(); 
    try { 
     fileout = new FileOutputStream(file); 
     fileout.write(thatString); 
     fileout.flush(); 
     fileout.close(); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 
+0

那麼,有什麼問題呢? – neevek

+1

那麼最新的問題?有什麼異常? – waqaslam

+8

您是否在您的清單中提供了權限 Aerrow

回答

0

我整天都在今天試圖弄清楚這一點,並我終於有了答案。只是以爲我會在這裏發佈它,以防其他人有類似的問題。我愚蠢地將我的許可寫入應用程序代碼塊而不是下面。應該是這樣的

<application android:icon="@drawable/icon" android:label="@string/app_name"> 
    <activity android:name=".Main" 
       android:label="@string/app_name"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
</application> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 

不喜歡這個

<application android:icon="@drawable/icon" android:label="@string/app_name"> 
    <activity android:name=".Main" 
       android:label="@string/app_name"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
</application>` 
0

也許你得到一個例外,你根本看不到它。取而代之的e.printStackTrace();,這是無用的,因爲它不DDMS顯示,使用L.e(TAG, "Error", e); 這樣,你應該能夠看到Exception,並找出什麼是錯的