简体   繁体   English

如何为我的手机中安装的 whatsapp、facebook 或其他应用程序创建共享按钮

[英]How to create a share button for whatsapp, facebook or others app installed in my phone

I am developing an app, where I want to share the TEXT and IMAGE to other apps installed on my phone.我正在开发一个应用程序,我想将 TEXT 和 IMAGE 分享给我手机上安装的其他应用程序。 But when I click the share button Neither it shares TEXT nor IMAGE just empty it directs me to another app I chose for sharing.但是,当我单击共享按钮时,它既不共享 TEXT 也不共享 IMAGE 只是清空它,它会将我定向到我选择共享的另一个应用程序。 below postDescription and postImage are my methods of model class, and I checked that am I getting values are not in a toast it gives there values properly.下面postDescriptionpostImage是我的 model class 的方法,我检查了我得到的值不是在吐司中,它正确地给出了值。

Below is the code:下面是代码:

    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"));

So the above bunch of code was not working, then I found code through which I can share my post TEXT and IMAGE to WhatsApp only, I tried that but it shows file formate not supported inside WhatsApp.所以上面的一堆代码不起作用,然后我找到了代码,通过它我可以只将我的帖子 TEXT 和 IMAGE 分享给 WhatsApp,我试过了,但它显示 WhatsApp不支持文件格式

below is the code for sharing to WhatsApp only:以下是仅分享给 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();
            }

I want to know how to create a share button that works fine for WhatsApp and everything or only WhatsApp will do the work for me.我想知道如何创建一个适用于 WhatsApp 和所有内容的共享按钮,或者只有 WhatsApp 才能为我完成工作。

By removing the package from the intent, Android should display the list of apps that can handle the type of intent.通过从 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM