Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging to a file on Android

Is there any way of retrieving log messages from an Android handset.

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.

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 Log.x("") to a text file so that I can see what the exception is.

Thanks

EDIT : Solution

Here is the code I finally went with...

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

I had to wrap the line

pw = new PrintWriter(new FileWriter(Environment.getExternalStorageDirectory()+"/rt.log", true)); 

in a try/catch as Eclipse would not let me compile the app. It kept saying

Unhandled exception type IOException  1 quick fix     Sorround with try/catch 

So I did and it all works which is fine by me but it does make me wonder what Eclipse was on about...

like image 264
Greg B Avatar asked Jan 22 '10 09:01

Greg B


People also ask

Is there a log file in Android?

Android allows collecting system logs using Logcat. Log messages can be viewed in a Logcat window in Android Studio, or you can use the command line tool to pull them. Several Android apps are also available in the Google Play store that allow easy access to these tools.

How do I view log files on Android?

Android 4.0 and older You can either download the SDK and use adb logcat or get Logcat Extrem from the Google Play Store, which shows the log directly on your phone. Alternatively, you can use Terminal Emulator with command "logcat > /sdcard/log. txt" for continuous writing of the log to a file on the SD Card.

Where are Android logs stored?

To access the logging output, run the 'adb' executable with following arguments to capture the Android Enterprise related logging: Windows: C:\Users\[username]\AppData\Local\Android\sdk\platform-tools> adb logcat -G 32M; adb shell setprop persist. log.


1 Answers

You could use Thread.setUncaughtExceptionHandler() to catch the Exceptions.

Writing to SD Card is as simple as retrieving the directory for the card using Environment.getExternalStorageDirectory() and creating a file there.

File f = new File(Environment.getExternalStorageDirectory(),filename); 

You will need to give you app the correct permission to write to the SD Card by adding this to your Manifest:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
like image 64
Dave Webb Avatar answered Oct 12 '22 15:10

Dave Webb