title: Logging to a file on Android
body: <p>Is there any way of retrieving log messages from an Android handset.</p>
  
  <p>I'm building an application which uses the GPS of my HTC Hero. I can run and debug the application from eclipse but this isn't a good use case of GPS, sat at my desk.</p>
  
  <p>When I fire the app up when I am walking around, I get an intermittent exception. Is there anyway I can output these exceptions to a text file on the SD card or output calls to <code>Log.x("")</code> to a text file so that I can see what the exception is.</p>
  
  <p>Thanks</p>
  
  <p><strong>EDIT : Solution</strong></p>
  
  <p>Here is the code I finally went with...</p>
  
  <pre><code>Thread.currentThread().setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
      @Override
      public void uncaughtException(Thread thread, Throwable ex) {
  
      PrintWriter pw;
      try {
          pw = new PrintWriter(
                  new FileWriter(Environment.getExternalStorageDirectory()+"/rt.log", true));
          ex.printStackTrace(pw);
          pw.flush();
          pw.close();
      } catch (IOException e) {
          e.printStackTrace();
      }
  }
  });
  </code></pre>
  
  <p>I had to wrap the line </p>
  
  <pre><code>pw = new PrintWriter(new FileWriter(Environment.getExternalStorageDirectory()+"/rt.log", true));
  </code></pre>
  
  <p>in a try/catch as Eclipse would not let me compile the app. It kept saying </p>
  
  <blockquote>
  <pre><code>Unhandled exception type IOException
  
  1 quick fix
      Sorround with try/catch
  </code></pre>
  </blockquote>
  
  <p>So I did and it all works which is fine by me but it does make me wonder what Eclipse was on about...</p>