title: Creating & sharing a simple text file in Android 10
body: <p>I've had some trouble creating a function that writes a large chunk of raw data to a text file, and then shares that text file to be sent by email/whatsapp. Since Android 10 it's no longer possible to use <code>Environment.getExternalStorageDir()</code> due to privacy reasons. It now seems that my file is not visible to other apps, and thus can not be opened by them. My code is as follows:</p>
  
  <p>Fragment responsible for exporting:</p>
  
  <pre><code>    CoroutineScope(Dispatchers.IO).launch {
              withContext(Dispatchers.IO) {
                  val data: String = retrieveData() ?: return@withContext
                  val file = File(requireContext().getExternalFilesDir(null), "export.xml")
                  val outputStream =
                      requireContext().openFileOutput("export.xml", Context.MODE_PRIVATE)
                  outputStream.write(data.toByteArray())
                  outputStream.close()
  
                  val uri = FileProvider.getUriForFile(requireContext(),BuildConfig.APPLICATION_ID, file)
  
                  val shareIntent = Intent().apply {
                      type = "text/plain"
                      action = Intent.ACTION_SEND
                      putExtra(Intent.EXTRA_STREAM, uri)
                      putExtra(Intent.EXTRA_SUBJECT, getString(R.string.share_file_subject))
                      addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
                  }
                  startActivity(
                      Intent.createChooser(
                          shareIntent,
                          getString(R.string.share_file_subject)
                      )
                  )
              }
          }
  </code></pre>
  
  <p>In the manifest:</p>
  
  <pre><code>        &lt;provider
              android:name="androidx.core.content.FileProvider"
              android:authorities="${applicationId}"
              android:exported="false"
              android:grantUriPermissions="true"&gt;
              &lt;meta-data
                  android:name="android.support.FILE_PROVIDER_PATHS"
                  android:resource="@xml/filepaths" /&gt;
          &lt;/provider&gt;
  </code></pre>
  
  <p>filepaths.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;paths&gt;
      &lt;external-files-path
          name="files"
          path="/" /&gt;
  &lt;/paths&gt;
  </code></pre>
  
  <p>When using the Android Studio file explorer I can see that the file is created with the correct content. The Uri points to: <code>content://myApplicationId/files/export.xml</code> and the file is located on my device at <code>data/data/myApplicationId/files</code> but I can't seem to figure out how to make it accessible for other apps to open.</p>
  
  <p>When opening it with for example the gmail app, it creates a new concept with the subject filled in but I'm getting a Toast from the gmail app saying 'Unable to add attachment'. The intent seems to fire properly so there are no exceptions occurring in my code. My guess is that the file is still not accessible for other apps for some reason.</p>
  
  <p>Any suggestions?</p>
  
  <p>Thanks in advance.</p>