1

I can read in my log what i wrote in a txt file in my sdcard but i can't open the file. 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:

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?

David_D
  • 1,404
  • 4
  • 31
  • 65

2 Answers2

2

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); 
}
Hariharan
  • 24,741
  • 6
  • 50
  • 54
1

Please try following code. Following code displays text file in an 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

surhidamatya
  • 2,419
  • 32
  • 56
  • this it display in a textview the data inside the txt file? i need something that open the file..in a android txt default viewer if possible – David_D Nov 12 '13 at 10:13
  • i forgot to initialize text as "String". I thought you wanted to open within app. Anyway you got the solution. Cheers!! – surhidamatya Nov 12 '13 at 10:49
  • 1
    I accept your answer anyway just 'cause you helped me ;). Bye – David_D Nov 12 '13 at 10:52