Skip to content

Instantly share code, notes, and snippets.

@cohenItay
Created June 27, 2022 17:51
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 cohenItay/52e6dedf4b3e45956493c67764ea5417 to your computer and use it in GitHub Desktop.
Save cohenItay/52e6dedf4b3e45956493c67764ea5417 to your computer and use it in GitHub Desktop.
fun shareAppLogs() {
val logsDirectory = logsFileProvider.provideLogsDirectory(appContext)
val innerFiles = logsDirectory.listFiles()
if (!logsDirectory.exists() || innerFiles.isNullOrEmpty()) {
Toast.makeText(appContext, "There are no logs to share", Toast.LENGTH_SHORT).show()
return
}
val urisToShare = innerFiles.map { file: File? ->
FileProvider.getUriForFile(
appContext,
"${BuildConfig.APPLICATION_ID}.contentprovider",
file!!
)
}
val intent = Intent(Intent.ACTION_SEND_MULTIPLE)
intent.type = "text/plain"
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, ArrayList(urisToShare))
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
try {
appContext.startActivity(intent)
} catch (e: ActivityNotFoundException) {
Log.e(MyAccountVM.TAG, "shareAppLogs: Cannot share logs because no activity found to handle intent", e)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment