1

I am creating an app which generates a csv file, and then sends it via email.

It does work if I choose Whatsapp, or Google Drive, but if I select Gmail, I get a toast "Unable to attach file". In the Logcat, I get the following lines:

2020-05-16 16:03:43.518 30779-30779/? W/Gmail: Gmail:No collectionId found for event forward
2020-05-16 16:03:43.518 30779-30779/? W/Gmail: Gmail:No itemId found for event forward

So I suppose it is a problem with my path, but I cannot manage to figure out what is the issue.

See the code below:

        String today = new SimpleDateFormat("yyyyMMdd_HHmm").format(Calendar.getInstance().getTime());

        //saving the file into device
        FileOutputStream out = openFileOutput("Data_"+today+".csv", Context.MODE_PRIVATE);
        out.write(data.toString().getBytes());
        out.close();

        //exporting
        Context context = getApplicationContext();
        File filelocation = new File(getFilesDir(), "Data_"+today+".csv");
        Uri path = FileProvider.getUriForFile(context, "com.aaaa.bbbb.fileprovider", filelocation);
        Intent fileIntent = new Intent(Intent.ACTION_SEND);
        fileIntent.setType("text/csv");
        String to[] = {"aaa@bbb.com"};
        fileIntent.putExtra(Intent.EXTRA_EMAIL,to);
        fileIntent.putExtra(Intent.EXTRA_SUBJECT, "Data_"+today+".csv");
        fileIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        fileIntent.putExtra(Intent.EXTRA_STREAM, path);

        Log.d(LOG_TAG,"filelocation = " + filelocation.toString());
        Log.d(LOG_TAG,"Uri = " + path.toString());

        startActivity(Intent.createChooser(fileIntent, "Send email"));

The file provider xml:

<paths>
<files-path
    name="data"
    path="."/>
</paths>

Here is the provider definition from the manifest:

<provider
    android:name="androidx.core.content.FileProvider"
    android:authorities="com.aaaa.bbbb.fileprovider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/provider_path" />
</provider>

The log.d provides the following:

2020-05-16 16:16:40.745 2030-2030/com.aaaa.bbbb D/Export: filelocation = /data/user/0/com.aaaa.bbbb/files/Data_20200516_1616.csv
2020-05-16 16:16:40.745 2030-2030/com.aaaa.bbbb D/Export: Uri = content://com.aaaa.bbbb.fileprovider/data/Data_20200516_1616.csv

Any idea of what is wrong?

RemiS
  • 11
  • 2

0 Answers0