2010-05-25 125 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...