1

I want to share an image from android activity to facebook messenger app which is already opened in my device and reside in recent tasks of the android device.

I am using the below code to share the image but it is opening a new activity in messenger 'share seperately' but I want to share it to the already opened chat.

String mimeType = "image/*";

Intent intent = new Intent(Intent.ACTION_SEND);
intent.setPackage("com.facebook.orca");
intent.setType(mimeType);
intent.putExtra(Intent.EXTRA_STREAM, contentUri);
intent.putExtra(EXTRA_PROTOCOL_VERSION, PROTOCOL_VERSION);
intent.putExtra(EXTRA_APP_ID, YOUR_APP_ID);

activity.startActivityForResult(shareIntent, SHARE_TO_MESSENGER_REQUEST_CODE);
prk
  • 11
  • 1

1 Answers1

0

Facebook has restricted the text sharing over the post. It will either support a valid URL link sharing or Image Sharing. So I've posted the code which is referenced from https://stackoverflow.com/a/22994833/8331006

private void shareAppLinkViaFacebook(String urlToShare) {
    try {
        Intent intent1 = new Intent();
        intent1.setClassName("com.facebook.katana", "com.facebook.katana.activity.composer.ImplicitShareIntentHandler");
        intent1.setAction("android.intent.action.SEND");
        intent1.setType("text/plain");
        intent1.putExtra("android.intent.extra.TEXT", urlToShare);
        startActivity(intent1);
    } catch (Exception e) {
        // If we failed (not native FB app installed), try share through SEND
        String sharerUrl = "https://www.facebook.com/sharer/sharer.php?u=" + urlToShare;
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(sharerUrl));
        startActivity(intent);
    }
}
BharathRao
  • 1,846
  • 1
  • 18
  • 28