英文:
How to create a share button for whatsapp, facebook or others app installed in my phone
问题
我正在开发一个应用程序,我希望在我的手机上安装的其他应用程序之间共享文本和图像。但是当我点击共享按钮时,它既不共享文本也不共享图像,只是空白,然后将我重定向到另一个我选择用于共享的应用程序。下面的postDescription和postImage是我的模型类的方法,我检查了一下,我是否能够得到这些值,在一个Toast中它正确地给出了这些值。
以下是代码:
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"));
所以上面这段代码没有起作用,然后我找到了一段代码,通过它我只能将我的帖子文本和图像分享到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未被安装。", Toast.LENGTH_SHORT).show();
}
我想知道如何创建一个可以在WhatsApp上正常工作的共享按钮,或者只有WhatsApp为我完成这项工作。
英文:
I am developing an app, where I want to share the TEXT and IMAGE to other apps installed on my phone.
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.
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.
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.
below is the code for sharing to WhatsApp only:
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.
答案1
得分: 0
通过从意图中移除包名,Android 应该会显示出可以处理该类型意图的应用程序列表。
Uri imgUri = Uri.parse(postImage); // 提供下载图片的 URI,而不是外部 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);
}
英文:
By removing the package from the intent, Android should display the list of apps that can handle the type of 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)
}
</details>
专注分享java语言的经验与见解,让所有开发者获益!
评论