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 ();}
Save Android program crash log to SD card