码迷,mamicode.com
首页 > 其他好文 > 详细

异常捕获和保存

时间:2016-02-23 20:39:01      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:


public void getLogToLocal(Throwable e) {
StringBuffer sb = new StringBuffer();
Writer write = new StringWriter();
PrintWriter print = new PrintWriter(write);
e.printStackTrace(print);
print.close();
String string = write.toString();

long currentTimeMillis = System.currentTimeMillis();
String format = sim.format(currentTimeMillis);
sb.append(format + "\n\r");
sb.append(string);

String fileName = "crash-" + format + "-" + currentTimeMillis + ".txt";
String file_dir = getFilePath();

File dir = new File(file_dir);
if (!dir.exists()) {
dir.mkdirs();
}
File file = new File(file_dir + fileName);
try {
if (!file.exists()) {
file.createNewFile();
}

FileOutputStream fos = new FileOutputStream(file);
fos.write(sb.toString().getBytes());

fos.close();

} catch (Exception e1) {
e1.printStackTrace();
}

}

private String getFilePath() {
String file_dir = "";

boolean isSDCardExist = Environment.MEDIA_MOUNTED.equals(Environment
.getExternalStorageState());

boolean isRootDirExist = Environment.getExternalStorageDirectory()
.exists();
if (isSDCardExist && isRootDirExist) {
file_dir = Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/openhapplog/";
} else {

file_dir = getApplicationContext().getFilesDir().getAbsolutePath()
+ "/openhappLog/";
}
return file_dir;
}

异常捕获和保存

标签:

原文地址:http://www.cnblogs.com/chenli123/p/5211009.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!