Skip to content

Instantly share code, notes, and snippets.

@zmdominguez
Last active July 8, 2019 08:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zmdominguez/be341240af8e9637ac6ffd5605665cc4 to your computer and use it in GitHub Desktop.
Save zmdominguez/be341240af8e9637ac6ffd5605665cc4 to your computer and use it in GitHub Desktop.
fun saveLogs() {
val debugDirectory = getStorageDirectory(this)
val logFile = File(debugDirectory, "logcat" + System.currentTimeMillis() + ".txt")
try {
var process = Runtime.getRuntime().exec("logcat -c")
process = Runtime.getRuntime().exec("logcat -f $logFile")
} catch (e: IOException) {
Timber.d(e)
}
}
fun sendLogs(context: Context) {
val allFiles = getFilesInDebugFolder(context)
val emailIntent = Intent(android.content.Intent.ACTION_SEND_MULTIPLE)
emailIntent.type = "plain/text"
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Shop App Log Dump")
val uri = arrayListOf<Uri>()
allFiles.forEach { file ->
uri.add(FileProvider.getUriForFile(context, "${BuildConfig.APPLICATION_ID}.provider", file))
}
emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uri)
try {
startActivity(Intent.createChooser(emailIntent, "Email logs"))
} catch (e: ActivityNotFoundException) {
Toast.makeText(activity, "No email clients available!", Toast.LENGTH_SHORT).show()
}
}
private fun getFilesInDebugFolder(context: Context): Array<out File> {
val storageDirectory = getStorageDirectory(context)
return storageDirectory.listFiles().orEmpty()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment