下面是将android anr产生的原因写到本地的方法。很好用。
/*
* 写入日志文件*/
public static File Log_saveFile(String crashReport) {
// TODO Auto-generated method stub
String fileName = "crash-" + System.currentTimeMillis() + ".txt";
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
try {
String path = Environment.getExternalStorageDirectory()
.getAbsolutePath()
+ File.separator
+ "AP10000"
+ File.separator + "Log";
File dir = new File(path);
if (!dir.exists()) {
dir.mkdirs(); // 新建多层文件夹
}
File file = new File(dir, fileName);
FileOutputStream fos = new FileOutputStream(file);
fos.write(crashReport.toString().getBytes());
fos.close();
return file;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}