5

I would like to display Log.i results in my application. Is it possible? If so, how can I do it?

Adam Lear
  • 38,111
  • 12
  • 81
  • 101
jehan
  • 267
  • 1
  • 4
  • 10
  • 1
    Welcome to stackoverflow! If you find an answer helpful, you can vote it up! If you feel that someone has adequately answered your question, click the check-mark next to the answer to accept it. – Kurtis Nusbaum Oct 23 '11 at 01:59

1 Answers1

15

Here's a blogpost that does exactly what you need it to do. It has a complete code example on how to display the contents of the Logcat log. Here's the code:

  import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import android.app.Activity;
  import android.os.Bundle;
  import android.widget.TextView;

  class ReadLogDemo extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);
            try {
              Process process = Runtime.getRuntime().exec("logcat -d");
              BufferedReader bufferedReader = new BufferedReader(
              new InputStreamReader(process.getInputStream()));

              StringBuilder log=new StringBuilder();
              String line = ""; 
              while ((line = bufferedReader.readLine()) != null) {
                log.append(line);
              }   
              TextView tv = (TextView)findViewById(R.id.textView1);
              tv.setText(log.toString());
            } catch (IOException e) {
          }
        }
} 
Kurtis Nusbaum
  • 30,445
  • 13
  • 78
  • 102
  • 1
    You might need permission to run Runtime.getRuntime().exec("logcat -d"); I think. exec causes problem from time to time due to permission issues. – Edison Oct 23 '11 at 02:12
  • its giving an error saying can't dispplay do v have to add anything in Manifest – jehan Oct 23 '11 at 02:16