在Android平台上同时发送文本和图片

3
在我的应用程序中,我的要求是同时发送图像和文本。因此,我使用以下代码。
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
share.putExtra(Intent.EXTRA_TEXT, "My photos");
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///"+f));                       
startActivity(Intent.createChooser(share, "Share Image"));

但是只有图片被发送了,文本没有发送。我该如何解决这个问题?

3个回答

2
您正在设置该意图的MIME类型image,因此只发送图像。像这样做将解决您的问题:
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("*/*");
share.putExtra(Intent.EXTRA_TEXT, "My photos");
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///"+f));                       
startActivity(Intent.createChooser(share, "Share Image"));

只有文件被发送,使用此方法不会发送文本。 - Renjith Krishnan

2

请尝试以下操作

//假设uris是一个Uri列表

Intent intent = null;
if (uris.size > 1){
intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
} else if (uris.size() == 1) {
intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_STREAM, uris.get(0));}
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_TEXT, "Some message");
startActivity(Intent.createChooser(intent,"compatible apps:"));

1
String message= "My photos";
URI = Uri.parse("file://" + f);
Intent share = new Intent(android.content.Intent.ACTION_SEND);
       share.setType("*/*");
       if (URI != null) {
        share.putExtra(Intent.EXTRA_STREAM, URI);
    }
       share.putExtra(android.content.Intent.EXTRA_TEXT, message);
startActivity(Intent.createChooser(share, "Share Image"));

这样应该没问题。

仅通过使用此方法发送文本,而不发送文件。 - Renjith Krishnan
我更新了我的答案。你能实现并让我知道吗? - Umit Kaya

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接