Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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