我的应用中有一个分享按钮,我想同时分享一张图片和一段文字。在 GMail 中它工作正常,但在 WhatsApp 中,只发送图像,在 Facebook 中应用程序崩溃。
我用来分享的代码是这样的:
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/*");
shareIntent.putExtra(Intent.EXTRA_TEXT, "Message");
Uri uri = Uri.parse("android.resource://" + getPackageName() + "/drawable/ford_focus_2014");
try {
InputStream stream = getContentResolver().openInputStream(uri);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
如果我使用“shareIntent.setType("*/*")” Facebook 和 WhatsApp 会崩溃。
有没有办法做到这一点?可能同时发送两条消息(WhatsApp)。
提前致谢。
最佳答案
目前 Whatsapp 同时支持图片和文字分享。 (2014 年 11 月)。
这是一个如何做到这一点的示例:
/**
* Show share dialog BOTH image and text
*/
Uri imageUri = Uri.parse(pictureFile.getAbsolutePath());
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
//Target whatsapp:
shareIntent.setPackage("com.whatsapp");
//Add text and then Image URI
shareIntent.putExtra(Intent.EXTRA_TEXT, picture_text);
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/jpeg");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
try {
startActivity(shareIntent);
} catch (android.content.ActivityNotFoundException ex) {
ToastHelper.MakeShortText("Whatsapp have not been installed.");
}
关于android - 通过 Whatsapp 或 Facebook 分享图片和文字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23077338/