首页 > 代码库 > 保存android程序崩溃日志到SD卡
保存android程序崩溃日志到SD卡
private boolean writeToSDCard(Throwable ex) { boolean isDealing = false; if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { RandomAccessFile randomAccessFile = null; try { String fileName = SDCARDROOT + File.separator + "logs" + File.separator + "crash" + File.separator; File file = new File(fileName); if(!file.exists()) file.mkdirs(); randomAccessFile = new RandomAccessFile(fileName + paserTime(System.currentTimeMillis())+ ".log", "rw"); long fileLength = randomAccessFile.length(); randomAccessFile.seek(fileLength); randomAccessFile.writeBytes(getThrowableInfo(ex)); } catch (IOException e) { e.printStackTrace(); } finally { if (randomAccessFile != null) { try { randomAccessFile.close(); isDealing = true; } catch (IOException e) { e.printStackTrace(); } } } } return isDealing; }
private static String getThrowableInfo(Throwable ex) { StringWriter stringWriter = new StringWriter(); PrintWriter printWriter = new PrintWriter(stringWriter); ex.printStackTrace(printWriter); return stringWriter.toString(); }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。