0

I used to attach a logs file, that I was storing in externalCacheDir, to a mail client. Using this : intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(logFile))

However, starting from Android 11 it just stopped working. So I tried using FileProvider. But once the mail client opens it says "Couldn't attach file".

I have tried doing everything I could find here on SO and on the internet in general. But I can't seem to find the solution that will actually work.

Activity :

val logFile: File = File(globalContext.externalCacheDir, "MyLogFile.log")

fun Activity.openMail(
    type: String = "text/plain"
): Boolean {
    val intent = Intent(Intent.ACTION_SENDTO)
    intent.putExtra(Intent.EXTRA_EMAIL, arrayOf("<some address...>"))
    intent.putExtra(Intent.EXTRA_SUBJECT, "Android Logs")
    intent.putExtra(Intent.EXTRA_TEXT, "some text")


    val uri = FileProvider.getUriForFile(this, "${BuildConfig.APPLICATION_ID}.fileprovider", logFile)
    intent.putExtra(Intent.EXTRA_STREAM, uri)

    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
    intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
    intent.type = type
    intent.data = Uri.parse("mailto:")

    if (intent.resolveActivity(packageManager) != null) {
        startActivity(Intent.createChooser(intent, "Send email via::"))
        return true
    } else {
        return false
    }
}

Manifest :

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<provider
    android:name="androidx.core.content.FileProvider"
    android:authorities="${applicationId}.fileprovider"
    android:exported="false"
    android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths" />
</provider>

provider_paths :

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-cache-path name="external_cache" path="."/>
</paths>
BVtp
  • 2,308
  • 2
  • 29
  • 68
  • `globalContext.externalCacheDir` Please put full path of directory there so we know where your file is. Also use a if(!logFile.exists()) return; Also display a Toast then. – blackapps Sep 24 '20 at 08:21
  • it's simply an Application context that assigned to a variable.. Also, the logFile does exist, I can see in the IDE and view its contents. – BVtp Sep 24 '20 at 10:43
  • Have you checked if the file exists or any error in the logs? – prashant17 Sep 24 '20 at 11:34
  • yes the file exists and I can see all the logs in it. It's the same way I always used this, and it always worked prior to Android 11. I just didn't use FileProvider for that, because I didn't have to. I only added FileProvider because of the changes in Android 11 – BVtp Sep 24 '20 at 13:35
  • @BVtp did you find any solution for this. I am also facing same now. Please share solution if find any solution – Dhanumjay Feb 16 '21 at 14:27
  • Same issue here. Any solution? – Henri L. Mar 06 '21 at 14:20
  • Unfortunately it seems there's no solution (or at least I didn't find one) that would allow to share files stored in private storage. I could only share files from external(shared) storage but even that was with opening the files "explorer", which is of course not what I wanted. – BVtp Mar 07 '21 at 07:37
  • Does this answer your question? [How to use support FileProvider for sharing content to other apps?](https://stackoverflow.com/questions/18249007/how-to-use-support-fileprovider-for-sharing-content-to-other-apps) – AgentP Aug 22 '21 at 03:02

0 Answers0