简体   繁体   English

为什么我无法在sd中读取文本文件?

[英]Why i can't read a textfile in sd?

I can read in my log what i wrote in a txt file in my sdcard but i can't open the file. 我可以在我的日志中读取我在sdcard中的txt文件中写的内容,但我无法打开该文件。 I need onClick open with a txt viewer or something else the file.. I can display in the log the values right now in this way: 我需要onClick打开一个txt查看器或其他文件..我可以通过这种方式在日志中显示值:

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());
            }

But i can't open the file.. how can i do it? 但我无法打开文件..我怎么能这样做?

Try this.. 尝试这个..

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); 
}

Please try following code. 请尝试以下代码。 Following code displays text file in an textedit. 以下代码在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);
        }

Hope this helps you!! 希望这对你有所帮助!! For more info you can go through android-read-text-file-from-sd-card 有关更多信息,您可以浏览android-read-text-file-from-sd-card

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM