![](/img/trans.png)
[英]How to share screenshot of libgdx game score in whatsapp and facebook?
[英]How to create a share button for whatsapp, facebook or others app installed in my phone
我正在开发一个应用程序,我想将 TEXT 和 IMAGE 分享给我手机上安装的其他应用程序。 但是,当我单击共享按钮时,它既不共享 TEXT 也不共享 IMAGE 只是清空它,它会将我定向到我选择共享的另一个应用程序。 下面postDescription和postImage是我的 model class 的方法,我检查了我得到的值不是在吐司中,它正确地给出了值。
下面是代码:
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
shareIntent.setType("image/*");
shareIntent.putExtra(Intent.EXTRA_TEXT, postDescription);
Uri uri = Uri.parse(postImage);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
context.startActivity(Intent.createChooser(shareIntent, "Share With"));
所以上面的一堆代码不起作用,然后我找到了代码,通过它我可以只将我的帖子 TEXT 和 IMAGE 分享给 WhatsApp,我试过了,但它显示 WhatsApp不支持文件格式。
以下是仅分享给 WhatsApp 的代码:
Uri imgUri = Uri.parse(postImage);
Intent whatsappIntent = new Intent(Intent.ACTION_SEND);
whatsappIntent.setType("text/plain");
whatsappIntent.setPackage("com.whatsapp");
whatsappIntent.putExtra(Intent.EXTRA_TEXT, postDescription );
whatsappIntent.putExtra(Intent.EXTRA_STREAM, imgUri);
whatsappIntent.setType("image/jpeg");
whatsappIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
try {
context.startActivity(whatsappIntent);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(context, "Whatsapp have not been installed.", Toast.LENGTH_SHORT).show();
}
我想知道如何创建一个适用于 WhatsApp 和所有内容的共享按钮,或者只有 WhatsApp 才能为我完成工作。
通过从 Intent 中删除 package,Android 应该显示可以处理 Intent 类型的应用程序列表。
Uri imgUri = Uri.parse(postImage); //Provide the URI to the downloaded image, not an external URL
Intent whatsappIntent = new Intent(Intent.ACTION_SEND);
whatsappIntent.setType("*/*");
whatsappIntent.putExtra(Intent.EXTRA_TEXT, postDescription );
whatsappIntent.putExtra(Intent.EXTRA_STREAM, imgUri);
if (whatsappIntent.resolveActivity(packageManager) != null) {
startActivity(whatsappIntent)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.