使用 FileProvider 时无法通过电子邮件发送文件

问题描述

美好的一天!我在通过电子邮件发送文件时遇到问题(我使用 FileProvider) 从下面的代码中,一切仅在我不附加文件时才有效 当我取消注释 emailIntent.putExtra(android.content.Intent.EXTRA_STREAM,uri) 行时,没有任何崩溃发送表单出现,但没有发送到给定地址 也许我遗漏了什么,请帮忙。

File file = new File(MainActivity.this.getCacheDir(),"test.txt");

    if (file.exists() == false);
    file.mkdirs();

    try {
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(MainActivity.this.openFileOutput("test.txt",Context.MODE_PRIVATE));
        for(int i=0;i<values.size();i++)
        outputStreamWriter.write(values.get(i)+"\n");
        outputStreamWriter.close();
    }
    catch (IOException e) {
        Log.e("Exception","File write Failed: " + e.toString());
    }

     Uri uri = FileProvider.getUriForFile(MainActivity.this,AUTHORITY,file);
    final Intent emailIntent = new Intent(
            android.content.Intent.ACTION_SEND);
    emailIntent.setType("message/rfc822");
    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,new String[] { "test@gmail.com" });
    emailIntent.putExtra(android.content.Intent.EXTRA_CC,new String[] {});
    emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"FROM Sample");
    emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,"HI");

 //  emailIntent.putExtra(android.content.Intent.EXTRA_STREAM,uri);

    MainActivity.this.startActivity(emailIntent);

文件提供者

 <provider
        android:name="androidx.core.content.FileProvider"
        android:authorities="app.testapp"
        android:exported="false"
        android:grantUriPermissions="true">
        <Meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths" />
    </provider>

provider_paths:

<paths xmlns:android="http://schemas.android.com/apk/res/android">

<files-path
    name="my_images"
    path="images/" />

<files-path
    name="my_docs"
    path="docs/" />

<external-files-path
    name="external_files_dir"
    path="/" />

<cache-path
    name="cache"
    path="/" />

<external-cache-path
    name="external_cache_dir"
    path="/" />

<external-media-path
    name="external_media_dir"
    path="/" />

<external-media-path
    name="external_media_dir"
    path="/" />

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)