content: package com.example.testijkplayer;
  
  
  import java.io.BufferedReader;
  import java.io.BufferedWriter;
  import java.io.Closeable;
  import java.io.FileNotFoundException;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.io.OutputStreamWriter;
  import java.io.PrintWriter;
  import java.io.StringWriter;
  import java.io.Writer;
  import java.lang.Thread.UncaughtExceptionHandler;
  
  
  import android.os.Environment;
  import android.text.format.DateFormat;
  import android.util.Log;
  
  public class snoCrashHandler implements UncaughtExceptionHandler {
  
      private static final String TAG = "snoCrashHandler";
  
      private UncaughtExceptionHandler defaultUEH;
  
      public snoCrashHandler() {
          this.defaultUEH = Thread.getDefaultUncaughtExceptionHandler();
      }
  
      @Override
      public void uncaughtException(Thread thread, Throwable ex) {
  
          final Writer result = new StringWriter();
          final PrintWriter printWriter = new PrintWriter(result);
  
          // Inject some info about android version and the device, since google can't provide them in the developer console
          StackTraceElement[] trace = ex.getStackTrace();
          StackTraceElement[] trace2 = new StackTraceElement[trace.length+3];
          System.arraycopy(trace, 0, trace2, 0, trace.length);
          trace2[trace.length+0] = new StackTraceElement("Android", "MODEL", android.os.Build.MODEL, -1);
          trace2[trace.length+1] = new StackTraceElement("Android", "VERSION", android.os.Build.VERSION.RELEASE, -1);
          trace2[trace.length+2] = new StackTraceElement("Android", "FINGERPRINT", android.os.Build.FINGERPRINT, -1);
          ex.setStackTrace(trace2);
  
          ex.printStackTrace(printWriter);
          String stacktrace = result.toString();
          printWriter.close();
          Log.e(TAG, stacktrace);
  
  //        // Save the log on SD card if available
  //        if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
  //            String sdcardPath = Environment.getExternalStorageDirectory().getPath();
  //            writeLog(stacktrace, sdcardPath + "/wifiplug_crash");
  //            writeLogcat(sdcardPath + "/wifiplug_logcat");
  //        }
  
          defaultUEH.uncaughtException(thread, ex);
      }
  
      private void writeLog(String log, String name) {
          CharSequence timestamp = DateFormat.format("yyyyMMdd_kkmmss", System.currentTimeMillis());
          String filename = name + "_" + timestamp + ".log";
  
          FileOutputStream stream;
          try {
              stream = new FileOutputStream(filename);
          } catch (FileNotFoundException e) {
              e.printStackTrace();
              return;
          }
  
          OutputStreamWriter output = new OutputStreamWriter(stream);
          BufferedWriter bw = new BufferedWriter(output);
  
          try {
              bw.write(log);
              bw.newLine();
          } catch (IOException e) {
              e.printStackTrace();
          }finally {
              close(bw);
              close(output);
          }
      }
  
      private void writeLogcat(String name) {
          CharSequence timestamp = DateFormat.format("yyyyMMdd_kkmmss", System.currentTimeMillis());
          String filename = name + "_" + timestamp + ".log";
          try {
              writeLogcat2(filename);
          } catch (IOException e) {
              Log.e(TAG, "Cannot write logcat to disk");
          }
      }
      private  boolean close(Closeable closeable) {
          if (closeable != null) {
              try {
                  closeable.close();
                  return true;
              } catch (IOException e) {
                  return false;
              }
          } else {
                  return false;
          }
      }
      public void writeLogcat2(String filename) throws IOException {
          String[] args = { "logcat", "-v", "time", "-d" };
  
          Process process = Runtime.getRuntime().exec(args);
  
          InputStreamReader input = new InputStreamReader(process.getInputStream());
  
          FileOutputStream fileStream;
          try {
              fileStream = new FileOutputStream(filename);
          } catch( FileNotFoundException e) {
              return;
          }
  
          OutputStreamWriter output = new OutputStreamWriter(fileStream);
          BufferedReader br = new BufferedReader(input);
          BufferedWriter bw = new BufferedWriter(output);
  
          try {
              String line;
              while ((line = br.readLine()) != null) {
                  bw.write(line);
                  bw.newLine();
              }
          }catch(Exception e) {}
          finally {
              close(bw);
              close(output);
              close(br);
              close(input);
          }
      }
  }
  