android - Is this below code is correct for saving logcat details to a file in eclipse? -
public static void savelogcattofile(context context) { string filename = "logcat_"+system.currenttimemillis()+".txt"; file outputfile = new file(context.getexternalcachedir(),filename); @suppresswarnings("unused") process process = runtime.getruntime().exec("logcat -f "+outputfile.getabsolutepath()); }
please check above code , me to save logcat details file (permanently), should append after everytime debugging in eclpise.
try
public static void savelogcattofile(context context) { try { process process = runtime.getruntime().exec("logcat -d"); bufferedreader bufferedreader = new bufferedreader(new inputstreamreader(process.getinputstream())); stringbuilder log = new stringbuilder(); string line; while ((line = bufferedreader.readline()) != null) { log.append(line); } string logfilepath = environment.getexternalstoragedirectory() + file.separator + "logtest.txt"; file logfile = new file(logfilepath); if (!logfile.exists()) logfile.createnewfile(); fileoutputstream outstream = new fileoutputstream(logfile, true); byte[] buffer = log.tostring().getbytes(); outstream.write(buffer); outstream.close(); } catch (exception ex) { ex.printstacktrace(); } }
and don't forget mention permissions in manifest file
<uses-permission android:name="android.permission.read_logs"/> <uses-permission android:name="android.permission.write_external_storage"/>
at last check so link writing file on external storage.
Comments
Post a Comment