/* In the calling activity class copy inside this onCreate() method */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (!(Thread.getDefaultUncaughtExceptionHandler() instanceof CustomExceptionHandler)) {
Thread.setDefaultUncaughtExceptionHandler(new CustomExceptionHandler(this));
}
}
/* =====================================================================================*/
/* CustomExceptionHandler.java */
package com.myapp.exception;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.Date;
public final class CustomExceptionHandler implements Thread.UncaughtExceptionHandler {
private final Thread.UncaughtExceptionHandler defaultUEH;
private final Context context;
private final String sdCardPath = Environment.getExternalStorageDirectory().getAbsolutePath();
private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
private static final String TAG = CustomExceptionHandler.class.getSimpleName();
public CustomExceptionHandler(final Context context) {
this.context = context.getApplicationContext();
this.defaultUEH = Thread.getDefaultUncaughtExceptionHandler();
}
@Override
public void uncaughtException(Thread thread, Throwable ex) {
final Writer result = new StringWriter();
final PrintWriter printWriter = new PrintWriter(result);
ex.printStackTrace(printWriter);
String stacktrace = result.toString();
printWriter.close();
String dirPath = sdCardPath + File.separator + "MyAppName" + File.separator;
String filePath = sdf.format(new Date()) + ".txt";
write(stacktrace, dirPath, filePath);
add(dirPath + filePath);
defaultUEH.uncaughtException(thread, ex);
}
/**
* Write Exception
*
* @param log String
* @param dirPath String
* @param filePath String
*/
private void write(final String log,
final String dirPath,
final String filePath) {
try {
File dir = new File(dirPath);
if (!dir.exists()) {
dir.mkdirs();
}
File file = new File(dir, filePath);
if (file.exists()) {
file.delete();
} else {
file.createNewFile();
file.setWritable(true, false);
}
BufferedWriter bos = new BufferedWriter(new FileWriter(file, true));
bos.write(log + "\n");
bos.flush();
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* ACTION_MEDIA_SCANNER_SCAN_FILE
*
* @param path String
*/
private void add(final String path) {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(path);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
context.sendBroadcast(mediaScanIntent);
}
}
One of the best snippets ever found to create an Android Custom Exception Handler that saves error logs to a prefered folder in device's SDCard.
#excepton #handler #custom #android
#cesarnog
#excepton #handler #custom #android
#cesarnog
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.