将SD卡中的txt文件的内容展示到EditText中

来源:互联网 发布:中国贸易顺差历年数据 编辑:程序博客网 时间:2024/05/22 14:18
1.先获取到SD卡目录下的所有Txt文件,保存到集合中
private List<File> getSDTxtFile() {    //1.获取到sd卡目录下所有的txt文件    List<File> txtFile = new ArrayList<>();    File file = new File(SDCardUtils.getSDCardPath());    TxtFileFilter txtFileFilter = new TxtFileFilter();    txtFileFilter.addType(".txt");    files = file.listFiles(txtFileFilter);    for (int i = 0; i < files.length; i++) {        Log.e("MyLog", "MeettingAgenda_Fragment.onClick:  文件 --->>> " + files[i]);        txtFile.add(files[i]);    }    return txtFile;}
获取SD路径
/** * 获取SD卡路径 * * @return */public static String getSDCardPath(){    return Environment.getExternalStorageDirectory().getAbsolutePath()            + File.separator;}
2.将获取到的txt文件展示并添加事件
/** *  以弹出框的形式展示查找到的txt文档文件 * @param txtFile 用来定义数组的大小 * @param edt 需要获取TXT文本内容的输入框 */private void showTxtDialog(List<File> txtFile, final EditText edt) {    //存放txt文件的路径    final String[] txtFilePath = new String[txtFile.size()];    //txt文件的名称    final String[] txtFileName = new String[txtFile.size()];    for (int i = 0; i < txtFile.size(); i++) {        txtFilePath[i] = txtFile.get(i).toString();//  /storage/emulated/0/游戏文本.txt        txtFileName[i] = txtFile.get(i).getName();//   游戏文本.txt    }    new AlertDialog.Builder(getContext()).setTitle("SD卡中所有的文档文件目录")            .setItems(txtFileName, new DialogInterface.OnClickListener() {                @Override                public void onClick(DialogInterface dialogInterface, int i) {                    Toast.makeText(getContext(), "点击了" + txtFileName[i], Toast.LENGTH_SHORT).show();                    // TODO: 2017/11/25 读取内容                    String s = ReadTxtFile(txtFilePath[i]);                    edt.setText(s);                }            }).create().show();}
读取txt文件的工具类
public String ReadTxtFile(String strFilePath) {    String path = String.valueOf(strFilePath);    String content = ""; //文件内容字符串    //打开文件    File file = new File(path);    //如果path是传递过来的参数,可以做一个非目录的判断    if (file.isDirectory()) {        Log.d("TestFile", "The File doesn't not exist.");    } else {        try {            InputStream instream = new FileInputStream(file);            if (instream != null) {                InputStreamReader inputreader = new InputStreamReader(instream);                BufferedReader buffreader = new BufferedReader(inputreader);                String line;                //分行读取                while ((line = buffreader.readLine()) != null) {                    content += line + "\n";                }                instream.close();            }        } catch (java.io.FileNotFoundException e) {            Log.d("TestFile", "The File doesn't not exist.");        } catch (IOException e) {            Log.d("TestFile", e.getMessage());        }    }    return content;}
3.使用:直接调用
case R.id.agenda_from:    showTxtDialog(getSDTxtFile(), mMeetAgendaEdt);    break;case R.id.notice_from:    showTxtDialog(getSDTxtFile(), mMeetNoticeEdt);    break;
阅读全文
0 0
原创粉丝点击