1.方式一:

Thread.UncaughtExceptionHandler handler = new Thread.UncaughtExceptionHandler() {    @Override    public void uncaughtException(Thread t, Throwable e) {        writeErrorLog(e);    }};protected void writeErrorLog(Throwable throwable) {    String info = null;    ByteArrayOutputStream outputStream = null;    PrintStream printStream = null;    try {        outputStream = new ByteArrayOutputStream();        printStream = new PrintStream(outputStream);        throwable.printStackTrace(printStream);        byte[] data = outputStream.toByteArray();        info = new String(data);        data = null;    } catch (Exception e) {        e.printStackTrace();    } finally {        try {            if (printStream != null) {                printStream.close();            }            if (outputStream != null) {                outputStream.close();            }        } catch (IOException e) {            e.printStackTrace();        }    }    File dir = new File(Error_Log);    if (dir.exists()) {        dir.mkdir();    }    File file = new File(dir, Log_Name);    try {        FileOutputStream fileOutputStream = new FileOutputStream(file, true);        fileOutputStream.write(info.getBytes());        fileOutputStream.close();    } catch (FileNotFoundException e) {        e.printStackTrace();    } catch (IOException e) {        e.printStackTrace();    }}

2.方式二:

依赖它 

compile 'com.github.wenmingvs:LogReport:1.0.3'
MyApplication中:
LogReport.getInstance()        .setCacheSize(30 * 1024 * 1024)//支持设置缓存大小,超出后清空        .setLogDir(getApplicationContext(), Environment.getExternalStorageDirectory().                getAbsolutePath() + "/" + this.getString(this.getApplicationInfo().labelRes) + "/")//定义路径为:sdcard/[app name]/        .setWifiOnly(true)//设置只在Wifi状态下上传,设置为false为Wifi和移动网络都上传        .setLogSaver(new CrashWriter(getApplicationContext()))//支持自定义保存崩溃信息的样式        //.setEncryption(new AESEncode()) //支持日志到AES加密或者DES加密,默认不开启        .init(getApplicationContext());

更多相关文章

  1. Android(安卓)Studio学习:简单控件:滚动显示文本
  2. android典型代码系列(二十五)------popupwindow的使用
  3. Android之Camera预览
  4. android 时间对话框
  5. Android设置RadioButton在文字的右边
  6. android 权限
  7. Android(安卓)Path的使用
  8. android 实现拍照有2种方式
  9. Android6.0修改设备设置中kernel信息

随机推荐

  1. Android uses-permission 权限大全
  2. Android常用Intent
  3. Android短信验证服务分享
  4. Android中ArrayList动态数组用法
  5. Android传感器(五):线性加速度传感器
  6. Android中常用的bitmap处理方法
  7. 7.1.3 TimePicker结合案例详解
  8. Android用户和用户组的定义
  9. Android 源码下载遇到 403错误 的解决办
  10. Android判断程序是否第一次启动