【发布时间】:2014-04-23 17:43:07
【问题描述】:
我的安卓 SD 卡上有一些文本文件,我需要访问其中一个。 我遇到了以下代码here:
//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');
}
}
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);
但是在这部分代码中:
//Get the text file
File file = new File(sdcard,"file.txt");
指定了文本文件的名称,但我需要用户选择他想要的文本文件(从 SD 卡中的那些文本文件中)。那么我怎样才能让用户浏览SD卡并选择 他想要的文件?
【问题讨论】: