在EXTRA_STREAM中传递的内容URI似乎对“收件人:”电子邮件栏位 [英] Content URI passed in EXTRA_STREAM appears to "To:" email field

查看:110
本文介绍了在EXTRA_STREAM中传递的内容URI似乎对“收件人:”电子邮件栏位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在缓存目录中创建一个文件,希望与他人共享(通过Gmail / WhatsApp等)。我可以使用FileProvider来做到这一点,并且对于WhatsApp来说效果很好。选择在Gmail上共享时,照片已正确附加,但我通过Intent.EXTRA_STREAM传递的Uri最终也被Gmail解析为新撰写的电子邮件的收件人:字段中的地址,以及该地址( es),我是通过Intent.EXTRA_EMAIL传递的。

I am creating a file in the cache directory that I'd like to share with others (via Gmail / WhatsApp etc). I am able to do this using FileProvider, and it works OK for WhatsApp. When choosing to share on Gmail the photo is correctly attached, but the Uri that I pass via Intent.EXTRA_STREAM also ends up being parsed by Gmail as an address in the "To:" field of the newly composed email, along with the address(es) that I pass via Intent.EXTRA_EMAIL.

因此,要求用户在发送之前删除虚假(Uri)电子邮件地址。知道如何防止这种情况发生吗?

So the user is required to delete the bogus (Uri) email address before sending. Any idea how to prevent this from happening?

Uri contentUri = FileProvider.getUriForFile(getActivity(), "com.mypackage.fileprovider", cacheFile);

Intent intent = new Intent(Intent.ACTION_SEND);
intent.setDataAndType(contentUri, "image/jpeg");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.putExtra(Intent.EXTRA_EMAIL, new String[] {"some_address@gmail.com"});
intent.putExtra(Intent.EXTRA_SUBJECT, "Photo");
intent.putExtra(Intent.EXTRA_TEXT, "Check out this photo");
intent.putExtra(Intent.EXTRA_STREAM, contentUri);

if(intent.resolveActivity(getActivity().getPackageManager()) != null)
{
    startActivity(Intent.createChooser(intent, getString(R.string.share_file)));
}


推荐答案

替换:

intent.setDataAndType(contentUri, "image/jpeg");

具有:

intent.setType("image/jpeg");

您的问题不是 EXTRA_STREAM ,而是您将 Uri 放入 Intent 的数据方面。

Your problem is not EXTRA_STREAM, but rather that you are putting the Uri in the Intent's data facet.

此外,如果您的 minSdkVersion 低于21,则需要使用一些额外的步骤,以确保客户端可以读取内容,如 Intent 标志在早期版本的Android上不会自动应用于 EXTRA_STREAM

Also, if your minSdkVersion is below 21, you will need to take some extra steps to ensure that clients can read the content, as the Intent flag is not applied to EXTRA_STREAM automatically on earlier versions of Android.

这篇关于在EXTRA_STREAM中传递的内容URI似乎对“收件人:”电子邮件栏位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆