Android 将文件附加到 GMAIL - 无法附加空文件

Android Attaching a file to GMAIL - Can't attach empty file

我有一个程序总是将相同的文件附加到 GMAIL(编写 > 附加文件 > 打开自 > "MyProgram")。它总是 select 同一个文件。

它正在做的是:

String path = Environment.getExternalStorageDirectory() + "/file.3gp";
File f = new File(path);
Uri data = Uri.fromFile(f);
Intent i = new Intent();
i.setData(data);
setResult(Activity.RESULT_OK, i);
finish();

这在 Android 6.0 之前工作正常。 现在,我在尝试使用它时收到以下错误:

Can't attach empty file

Astro 文件共享给我同样的错误(可能是旧版本)。

但是,我安装了 ES 文件资源管理器,当我执行相同的例程和 select 文件时,我收到一个对话框,上面写着:

Pick up file as

  • Normal Android Way (For MMS,Gmail,...)
  • File Way (Try this if above fails)

"File Way" 会像我的程序一样失败。 "Normal Android Way" 可以正常工作。

有没有人知道它的作用,所以我可以复制?

提前致谢!

OBS:已经多次尝试putExtra(STREAM, path),但没有成功。

好的,经过大量研究并拦截了一些 Intent,现在开始工作了。

我必须做的是将 file:/// 更改为 content://。

我是根据 Android 中的信息执行此操作的:https://developer.android.com/reference/android/support/v4/content/FileProvider.html

唯一的主要变化是我使用了 /sdcard/file.ext 的硬编码路径。 此外,行

getUriForFile(getContext(), "com.mydomain.fileprovider", newFile);

已更改为

Uri contentUri = FileProvider.getUriForFile(this, "com.mydomain.fileprovider", newFile);

还必须包括:

intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
i.setData(contentUri);

我真的不明白为什么我必须将从文件更改为内容,但在此之后,文件现在又被附加了!如果您遇到此问题,请参阅 link,并且不要忘记需要创建的新 .xml。

请看下面的问题:

此行为适用于 Android 6.0 和 Gmail。请参阅以下主题。

Issue 3141 - android-developer-preview

If you go to Settings->Apps->Gmail->Permissions and enable the "Storage" permission manually, then the share works.

Gmail should ask for "Storage" permission in this scenario and it would work as it did in all the past version of Android.

其他电子邮件应用程序应正确处理附件。

这是解决方法。

转到设置 -> 应用程序 -> Gmail -> 权限 开启 "Storage" 的权限 该解决方法为我解决了这个问题。

我找不到明确的答案(在没有SD卡的情况下在gmail上发送附件)我试图复制到另一个文件名但没有雪茄。我让它工作的方法是复制到下载文件夹,然后从那里开始。

用这个获取下载路径 Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)

拉取下载文件夹的完整路径,将附件复制到那里,我们就可以了。超过 20M 的文件不去

使用给定文件路径提供的 INTENT 共享任何文件

    //File file= shareable File Path
    Uri uri = FileProvider.getUriForFile(this, "com.example.myapp.fileprovider", file); 
    //FileProvider authorities from Manifest
            
    Intent sharingIntent = new Intent(Intent.ACTION_SEND);
    sharingIntent.setType("*/*");

    sharingIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    sharingIntent.putExtra(Intent.EXTRA_STREAM, uri);
    startActivity(sharingIntent);

只需在方法 getUriForFile.

的参数中匹配 manifest 中的 authorities

在清单中

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