1

我正在创建一个生成 csv 文件的应用程序,然后通过电子邮件发送它。

如果我选择 Whatsapp 或 Google Drive,它确实有效,但如果我选择 Gmail,我会收到“无法附加文件”的提示。在 Logcat 中,我得到以下几行:

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

所以我想这是我的路径的问题,但我无法弄清楚问题是什么。

请看下面的代码:

        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"));

文件提供者 xml:

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

这是清单中的提供程序定义:

<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>

log.d 提供以下内容:

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

知道出了什么问题吗?

4

0 回答 0