2010-05-25 104 views
59

我是Android開發新手。如何從Android中的SD卡讀取文本文件?

我需要從SD卡讀取文本文件並顯示該文本文件。 有沒有什麼方法可以直接在Android中查看文本文件,或者我如何讀取和顯示文本文件的內容?

+0

你想知道如何編寫一個程序,讀取一個txt文件或者你想知道如何做一個用戶? – SteelBytes 2010-05-25 07:28:52

回答

120

In your layout你需要一些東西來顯示文本。 A TextView是明顯的選擇。所以你有這樣的事情:

<TextView 
    android:id="@+id/text_view" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"/> 

您的代碼看起來就像這樣:

//Find the directory for the SD Card using the API 
//*Don't* hardcode "/sdcard" 
File sdcard = Environment.getExternalStorageDirectory(); 

//Get the text file 
File file = new File(sdcard,"file.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'); 
    } 
    br.close(); 
} 
catch (IOException e) { 
    //You'll need to add proper error handling here 
} 

//Find the view by its id 
TextView tv = (TextView)findViewById(R.id.text_view); 

//Set the text 
tv.setText(text); 

這可能會去你的ActivityonCreate()方法,或別的地方取決於究竟有什麼你想要做什麼。

+0

不工作,它返回文本爲空 – 2011-07-21 08:27:24

+1

@Android開發人員 - 在示例代碼中,它說「你需要在這裏添加適當的錯誤處理」。你是否這樣做是因爲這可能是你最容易找到問題的地方。 – 2011-07-21 15:01:00

+0

@Dave Webb抱歉沒有更新我的評論。它也適用於我。實際上,當我將文件放入DDMS中時,它將清除文件中的所有數據,這就是爲什麼它在我的結尾處返回null。 – 2011-07-22 04:04:56

5

針對

不要硬編碼/ SD卡/

有時候我們必須硬編碼在某些型號的手機API方法返回內部手機內存。

已知類型:HTC One X和三星S3。

Environment.getExternalStorageDirectory().getAbsolutePath() gives a different path - Android

+0

中爲我添加使用權限:'READ_EXTERNAL_STORAGE',它給了一些安全agumentation垃圾/com.app.name/data/sdcard/ ...請注意。對於我自己的日誌,我終於可以編寫代碼了。 – 2015-01-29 00:43:43

0
package com.example.readfilefromexternalresource; 

import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.io.IOException; 

import android.app.Activity; 
import android.app.ActionBar; 
import android.app.Fragment; 
import android.os.Bundle; 
import android.os.Environment; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.TextView; 
import android.widget.Toast; 
import android.os.Build; 

public class MainActivity extends Activity { 
    private TextView textView; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     textView = (TextView)findViewById(R.id.textView); 
     String state = Environment.getExternalStorageState(); 

     if (!(state.equals(Environment.MEDIA_MOUNTED))) { 
      Toast.makeText(this, "There is no any sd card", Toast.LENGTH_LONG).show(); 


     } else { 
      BufferedReader reader = null; 
      try { 
       Toast.makeText(this, "Sd card available", Toast.LENGTH_LONG).show(); 
       File file = Environment.getExternalStorageDirectory(); 
       File textFile = new File(file.getAbsolutePath()+File.separator + "chapter.xml"); 
       reader = new BufferedReader(new FileReader(textFile)); 
       StringBuilder textBuilder = new StringBuilder(); 
       String line; 
       while((line = reader.readLine()) != null) { 
        textBuilder.append(line); 
        textBuilder.append("\n"); 

       } 
       textView.setText(textBuilder); 

      } catch (FileNotFoundException e) { 
       // TODO: handle exception 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      finally{ 
       if(reader != null){ 
        try { 
         reader.close(); 
        } catch (IOException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
       } 
      } 

     } 
    } 
} 
1
BufferedReader br = null; 
try { 
     String fpath = Environment.getExternalStorageDirectory() + <your file name>; 
     try { 
      br = new BufferedReader(new FileReader(fpath)); 
     } catch (FileNotFoundException e1) { 
      e1.printStackTrace(); 
     } 
     String line = ""; 
     while ((line = br.readLine()) != null) { 
     //Do something here 
     } 
+0

你能否提供一個解釋給你的代碼請..如何使用它,爲什麼這樣做的工作,等等。 – Soma 2015-05-13 11:32:59

0

請注意:有些手機具有2個sdcards,內部固定一個可移動卡。 您可以通過標準應用程序找到最後一個的名稱:「Mijn Bestanden」(英文:「MyFiles」?) 當我打開此應用程序(項目:所有文件)時,打開文件夾的路徑爲「/ sdcard 「,向下滾動則會出現一個條目」external-sd「,單擊該條目將打開文件夾」/ sdcard/external_sd /「。 假設我想打開一個文本文件「MyBooks.txt」我會用的東西如:

String Filename = "/mnt/sdcard/external_sd/MyBooks.txt" ; 
    File file = new File(fname);...etc...