I am trying to attach pdf to Gmail using file provider. It is working on Android 6.0 but says 'Couldn't attach file'
fun startFileShareIntent(filePath: String, context: Context?) {
try {
val file = File(filePath)
val uri = FileProvider.getUriForFile(context!!, "com.trust.inspakt.android.provider", file)
val intent = ShareCompat.IntentBuilder.from(context as Activity)
.setType("application/pdf")
.setStream(uri)
.setChooserTitle("Choose bar")
.createChooserIntent()
.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
context.startActivity(intent)
} catch (e: Exception) {
e.printStackTrace()
}
}
I assume from your question that it is working on Android 7.0 and up?
In Android 6.0 there is no need for a file provider. Here is the Java code that I am using to attach a csv file to gmail. This works for me from Android 6.0 up to Android 11.