---
title: "How do I obtain crash-data from my Android application?"  
description: "How do I obtain crash-data from my Android application?"  
author: "Anonymous User"  
published: 2015-06-08  
updated: 2015-06-08  
canonical: https://www.mindstick.com/forum/23285/how-do-i-obtain-crash-data-from-my-android-application  
category: "android"  
tags: ["android"]  
reading_time: 5 minutes  

---

# How do I obtain crash-data from my Android application?

How can I get crash [data](https://www.mindstick.com/articles/13050/salesforce-aiming-to-dominate-predictive-analytics-with-data-science) ([stack](https://www.mindstick.com/blog/301746/why-is-stack-overflow-so-important-for-developers) traces at [least](https://yourviews.mindstick.com/story/1471/5-autobiographies-you-should-read-at-least-once)) from my [Android application](https://www.mindstick.com/forum/33405/how-to-get-the-build-version-number-of-your-android-application)? At least when working on my own [device](https://www.mindstick.com/blog/149/retriving-network-device-information-in-c-sharp) being retrieved by cable, but ideally from any [instance](https://www.mindstick.com/forum/155658/testing-connection-to-cloud-sql-instance-with-a-mysql-client-from-vm-instance) of my application running on the wild so that I can improve it and make it more solid.

## Replies

### Reply by Anonymous User

For sample applications and debugging purposes, I use a simple solution that allows me to write the stacktrace to the sd card of the device and/or upload it to a server. This solution has been inspired by http://code.google.com/p/android-remote-stacktrace (specifically, the save-to-device and upload-to-server parts) and I think it solves the problem mentioned by Soonil. It's not optimal, but it works and you can improve it if you want to use it in a production [application](https://www.mindstick.com/articles/12824/calculator-application-in-android). If you decide to upload the stacktraces to the server, you can use a php script (index.php) to view them. If you're interested, you can find all the sources below - one java class for your application and two optional php scrips for the server hosting the uploaded stacktraces.\
In a Context (e.g. the main Activity), call\

```
if(!(Thread.getDefaultUncaughtExceptionHandler() instanceof CustomExceptionHandler)) {    Thread.setDefaultUncaughtExceptionHandler(new CustomExceptionHandler(            "/sdcard/<desired_local_path>", "http://<desired_url>/upload.php"));}
```

\
CustomExceptionHandler\

```
public class CustomExceptionHandler implements UncaughtExceptionHandler {    private UncaughtExceptionHandler defaultUEH;    private String localPath;    private String url;    /*      * if any of the parameters is null, the respective functionality      * will not be used      */    public CustomExceptionHandler(String localPath, String url) {        this.localPath = localPath;        this.url = url;        this.defaultUEH = Thread.getDefaultUncaughtExceptionHandler();    }    public void uncaughtException(Thread t, Throwable e) {        String timestamp = TimestampFormatter.getInstance().getTimestamp();        final Writer result = new StringWriter();        final PrintWriter printWriter = new PrintWriter(result);        e.printStackTrace(printWriter);        String stacktrace = result.toString();        printWriter.close();        String filename = timestamp + ".stacktrace";        if (localPath != null) {            writeToFile(stacktrace, filename);        }        if (url != null) {            sendToServer(stacktrace, filename);        }        defaultUEH.uncaughtException(t, e);    }    private void writeToFile(String stacktrace, String filename) {        try {            BufferedWriter bos = new BufferedWriter(new FileWriter(                    localPath + "/" + filename));            bos.write(stacktrace);            bos.flush();            bos.close();        } catch (Exception e) {            e.printStackTrace();        }    }    private void sendToServer(String stacktrace, String filename) {        DefaultHttpClient httpClient = new DefaultHttpClient();        HttpPost httpPost = new HttpPost(url);        List<NameValuePair> nvps = new ArrayList<NameValuePair>();        nvps.add(new BasicNameValuePair("filename", filename));        nvps.add(new BasicNameValuePair("stacktrace", stacktrace));        try {            httpPost.setEntity(                    new UrlEncodedFormEntity(nvps, HTTP.UTF_8));            httpClient.execute(httpPost);        } catch (IOException e) {            e.printStackTrace();        }    }}
```

\
upload.php

```
<?php    $filename = isset($_POST['filename']) ? $_POST['filename'] : "";    $message = isset($_POST['stacktrace']) ? $_POST['stacktrace'] : "";    if (!ereg('^[-a-zA-Z0-9_. ]+$', $filename) || $message == ""){        die("This script is used to log debug data. Please send the "                . "logging message and a filename as POST variables.");    }    file_put_contents($filename, $message . "\n", FILE_APPEND);?>
```

\
index.php

```
<?php    $myDirectory = opendir(".");    while($entryName = readdir($myDirectory)) {        $dirArray[] = $entryName;    }    closedir($myDirectory);    $indexCount = count($dirArray);    sort($dirArray);    print("<TABLE border=1 cellpadding=5 cellspacing=0 \n");    print("<TR><TH>Filename</TH><TH>Filetype</th><th>Filesize</TH></TR>\n");    for($index=0; $index < $indexCount; $index++) {        if ((substr("$dirArray[$index]", 0, 1) != ".")                 && (strrpos("$dirArray[$index]", ".stacktrace") != false)){             print("<TR><TD>");            print("<a href=\"$dirArray[$index]\">$dirArray[$index]</a>");            print("</TD><TD>");            print(filetype($dirArray[$index]));            print("</TD><TD>");            print(filesize($dirArray[$index]));            print("</TD></TR>\n");        }    }    print("</TABLE>\n");?>
```

### Reply by Anonymous User

It is possible to handle these exceptions with Thread.setDefaultUncaughtExceptionHandler(), however this appears to mess with [Android](https://www.mindstick.com/articles/13046/10-reasons-why-i-prefer-android-vs-iphone-ios)'s method of handling exceptions. I attempted to use a handler of this nature:

```
private class ExceptionHandler implements Thread.UncaughtExceptionHandler {    @Override    public void uncaughtException(Thread thread, Throwable ex){        Log.e(Constants.TAG, "uncaught_exception_handler: uncaught exception in thread " + thread.getName(), ex);        //hack to rethrow unchecked exceptions        if(ex instanceof RuntimeException)            throw (RuntimeException)ex;        if(ex instanceof Error)            throw (Error)ex;        //this should really never happen        Log.e(Constants.TAG, "uncaught_exception handler: unable to rethrow checked exception");    }}
```

However, even with rethrowing the exceptions, I was unable to get the desired behavior, ie logging the exception while still allowing Android to shutdown the component it had happened it, so I gave up on it after a while.


---

Original Source: https://www.mindstick.com/forum/23285/how-do-i-obtain-crash-data-from-my-android-application

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
