2013-07-21 51 views
-1

您好所有Android上的外部數據存儲的教程都driveing我堅果Android的數據extstorage:快速幫助請

我有一個OnOptionsCreatemenu有2個按鈕「保存」,「加載」保存工作正常 但負載不會在這裏是我的代碼,它不會讓我iv在代碼 下注釋也如何讓我把它保存在我自己的目錄中?... iv閱讀每個單獨的教程保存到5分鐘的ext存儲.....即時通訊只是愚蠢的得到它! (請不要鏈接) 然後請把它變成一個新的教程,因爲它將是最簡單的android ext數據存儲故障,甚至像我這樣的遲鈍的黑猩猩會明白

這是所有的意思都是一塊代碼。 .....不知道爲什麼代碼顯示在2個代碼框?

P.S對不起它我第一次.....很容易;)

私人無效保存(){

String saveLine = ((EditText) 
      findViewById(R.id.captain)).getText().toString().trim(); 

    File sdcard = Environment.getExternalStorageDirectory(); 

    File file = new File(sdcard,"/myfile.txt"); 

    try { 

     FileWriter fw = new FileWriter(file); 

     BufferedWriter bw = new BufferedWriter(fw); 

     bw.write(saveLine); 

     bw.close(); 
     Toast.makeText(getApplicationContext(),"File saved!",Toast.LENGTH_LONG).show(); 
    } catch (IOException e) { 

     Toast.makeText(getApplicationContext(),"Error writing file!",Toast.LENGTH_LONG).show(); 

    } 
    { 

//這裏說,我需要把一個變量?爲什麼不讓它像save()函數一樣使用它?

private void load() { 

     // ensure(); 

     sdcard = Environment.getExternalStorageDirectory(); 

     File file1 = new File(sdcard,"/myfile.txt"); 


     try { 

      BufferedReader br = new BufferedReader(new FileReader(file1)); 

      String loadline; 

      if ((loadline = br.readLine()) != null) { 

       String content=loadline.toString(); 

       ((EditText) 
         findViewById(R.id.captain)).setText(content); 



      } 

      br.close(); 

     } 

     catch (IOException e) { 

      Toast.makeText(getApplicationContext(),"Error reading file!",Toast.LENGTH_LONG).show(); 

     } 

    } 
+0

只是爲了將來的知識,不要把你的帖子中的幫助和類似的東西,因爲他們皺起了眉頭。除此之外,歡迎來到SO。 – ObieMD5

+0

好吧,我不知道 – OnTHEvERGEOFaANURYSM

回答

0

您似乎有閱讀文件的問題,對吧?用於讀取SD卡中的文件並將其設置爲EditText這裏是代碼:

private void load(){ 

    File sdcard = Environment.getExternalStorageDirectory(); 

    //Get the text file 
    File file = new File(sdcard,"myfile.txt"); 

    //Read text from file 
    StringBuilder text = new StringBuilder(); 

    try { 
     BufferedReader br = new BufferedReader(new FileReader(file)); 
     String line; 

     while ((line = br.readLine()) != null) { 
      text.append(line); 
      text.append('\n'); 
     } 
    } 
    catch (IOException e) { 
     //You'll need to add proper error handling here 
     Toast.makeText(getApplicationContext(),"Error reading file!",Toast.LENGTH_LONG).show(); 
    } 

    //Find the view by its id 
    EditText ed = (EditText)findViewById(R.id.captain); 

    //Set the text 
    ed.setText(text); 
} 

確切來源:How can I read a text file from the SD card in Android?

希望這有助於其他評論。

+0

好吧,所以即時做私人空載(){.....然後你的代碼,然後它會拋出負載和()之間的「表達預期」....感謝您的幫助提前。 。我是否嘗試將負載用作自己的方法?像保存() – OnTHEvERGEOFaANURYSM

+0

清理和重建代碼沒有幫助? –

+0

當我重建項目它說:「java:非法啓動的表達式」,然後我可以跳轉到源,它需要我私人空載(){....它仍然需要一個表達式?即時通訊也使用智能j在OnOpyionsCreateMenu情況下爲load()說它無法解析方法?? – OnTHEvERGEOFaANURYSM