We are now testing our application with a few friends. Sometimes there are some errors which don\'t throw an exception. So I don\'t really know whats the problem was. So I t
In your manifest file give the following permissions:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
In your first/launcher activity, right after the
super.onCreate(savedInstanceState);
Write below lines: This will write your App's logcat to your device's external storage
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, EXTERNAL_PERMISSION_CODE);
}
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, READ_PERMISSION_CODE);
}
if ( isExternalStorageWritable() ) {
File appDirectory = new File( Environment.getExternalStorageDirectory() + "/MyAppLog" );
File logDirectory = new File( appDirectory + "/log" );
File logFile = new File( logDirectory, "logcat" + ".txt" );
// create app folder
if ( !appDirectory.exists() ) {
appDirectory.mkdir();
}
// create log folder
if ( !logDirectory.exists() ) {
logDirectory.mkdir();
}
// clear the previous logcat and then write the new one to the file
if ( logFile.exists()){
logFile.delete();
}
try {
Process process = Runtime.getRuntime().exec("logcat -c");
process = Runtime.getRuntime().exec("logcat -f " + logFile);
} catch ( IOException e ) {
e.printStackTrace();
}
} else if ( isExternalStorageReadable() ) {
// only readable
} else {
// not accessible
}
For sending the logcat to desired email address: use below method
public void sendLogcatMail(){
if ( isExternalStorageWritable() ) {
File appDirectory = new File(Environment.getExternalStorageDirectory() + "/MyAppLog");
File logDirectory = new File(appDirectory + "/log");
File logFile = new File(logDirectory, "logcat" + ".txt");
if (logFile.exists()) {
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("vnd.android.cursor.dir/email");
String to[] = {"yourEmailAddress@gmail.com"};
emailIntent.putExtra(Intent.EXTRA_EMAIL, to);
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(String.valueOf(logFile.toURI())));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Log files");
emailIntent.putExtra(Intent.EXTRA_TEXT, "Send some message along");
startActivity(Intent.createChooser(emailIntent, "Send email..."));
}
}
}
Method for checking whether the permissions for Writing to External storage is given or not:
/* Checks if external storage is available for read and write */
public static boolean isExternalStorageWritable() {
String state = Environment.getExternalStorageState();
if ( Environment.MEDIA_MOUNTED.equals( state ) ) {
return true;
}
return false;
}