Created
September 9, 2015 11:45
-
-
Save asanand3/16edc8162d2e3e77c3dc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Read the log through the following code: | |
public class LogTest extends Activity { | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.main); | |
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); | |
} | |
TextView tv = (TextView)findViewById(R.id.textView1); | |
tv.setText(log.toString()); | |
} catch (IOException e) { | |
} | |
} | |
} | |
The above code reads the log and displays it in a textview. | |
You should define: | |
<uses-permission android:name="android.permission.READ_LOGS" /> | |
permission in your manifest. | |
To copy the log directly to a file,use the following method: | |
public static void copyLogcatToFile(Context context) { | |
String fileName = "log_cat"+System.currentTimeMillis()+".txt"; | |
File outputFile = new File(context.getExternalCacheDir(),fileName); | |
@SuppressWarnings("unused") | |
Process process = Runtime.getRuntime().exec("logcat -f "+outputFile.getAbsolutePath()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment