1

我可以在我的日志中读取我在 sdcard 的 txt 文件中写的内容,但我无法打开该文件。我需要使用 txt 查看器或其他文件打开 onClick。我现在可以通过这种方式在日志中显示值:

public void onClick(View v) 
            {

                File file = new File("/sdcard/ReportData.txt");
                StringBuffer contents = new StringBuffer();
                BufferedReader reader = null;

                try {
                    reader = new BufferedReader(new FileReader(file));
                    String text = null;

                    // repeat until all lines is read
                    while ((text = reader.readLine()) != null) {
                        contents.append(text)
                            .append(System.getProperty(
                                "line.separator"));
                    }
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        if (reader != null) {
                            reader.close();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }           

                Log.e("TEXT", contents.toString());
            }

但是我无法打开文件..我该怎么做?

4

2 回答 2

2

尝试这个..

public void onClick(View v) 
{
          Intent intent = new Intent();
          intent.setAction(android.content.Intent.ACTION_VIEW);
          File file = new File("/sdcard/ReportData.txt");
          intent.setDataAndType(Uri.fromFile(file), "text/*");
          startActivity(intent); 
}
于 2013-11-12T09:30:13.007 回答
1

请尝试以下代码。以下代码在 textedit 中显示文本文件。

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

public void onClick(View v) 
        {

            File file = new File("/sdcard/ReportData.txt");
            StringBuffer contents = new StringBuffer();
            BufferedReader reader = null;

            try {
                reader = new BufferedReader(new FileReader(file));
                String text = null;

                // repeat until all lines is read
                while ((text = reader.readLine()) != null) {
                    contents.append(text)
                        .append(System.getProperty(
                            "line.separator"));
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (reader != null) {
                        reader.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }           

            Log.e("TEXT", contents.toString());
             String text = contents.toString();

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

希望这对你有帮助!!有关更多信息,您可以通过android-read-text-file-from-sd-card

于 2013-11-12T10:07:56.253 回答