3

Background
In my Android App, users can share generated images to other apps. It's working nicely using the ACTION_SEND Intent.
Many users have asked why they can't share to Instagram stories directly.

Initially I thought Instagram doesn't support receiving Intents for stories (correct to some extent). I searched for it today, and according to this documentation, to share to Instagram Stories, a separate intent com.instagram.share.ADD_TO_STORY has to be used. I tried it, and it works fine.

The problem:
How do I keep both the options available?

I thought about it a lot, and came up with the following options:

1) Have two separate buttons. It will work, but it will look/feel bad.

2) Have my app accept ACTION_SEND intent, name it as Share to Instagram Story, and redirect the intent to the com.instagram.share.ADD_TO_STORY intent. In principle, make a proxy intent.
It will work, and look/feel great, but I don't know if its allowed (legal, etc) and can I disable the intent if the user doesn't have Instagram installed.

3) Add the 'com.instagram.share.ADD_TO_STORY' to the app chooser launched by ACTION_SEND. This would be ideal, but I don't know how to do it.

Haider Ali Punjabi
  • 345
  • 1
  • 3
  • 13
  • I think the proxy intent works good. You can check if instagram is installed in phone and disable/enable intent accordingly. https://stackoverflow.com/a/5016624/6414086 , check if instagram is installed by using the function in the linked answer. The package name for instagram is "com.instagram.android" – emilpmp Sep 13 '18 at 12:05
  • @emilpmp I also think so. If no one is able to help me with option 3, I hope someone atleast tells me how I can disable the intent when Instagram app is not installed – Haider Ali Punjabi Sep 13 '18 at 12:07
  • edited my comment. Just use ADD_TO_STORY intent if instagram is installed or else use the ACTION_SEND intent. The function to find out if a package is installed on phone is in my first comment. – emilpmp Sep 13 '18 at 12:09
  • @emilpmp I think you understood it wrong. Even if instagram is installed, I still want the `ACTION_SEND` to work. Any ways, I accepted an answer below – Haider Ali Punjabi Sep 13 '18 at 12:54

3 Answers3

4

If you want to add multiple actions to Intent and create a chooser look at this example:

Intent viewIntent = new Intent(Intent.ACTION_VIEW);
Intent editIntent = new Intent(Intent.ACTION_EDIT);
viewIntent.setDataAndType(uri, type);
editIntent.setDataAndType(uri, type);
Intent chooserIntent = Intent.createChooser(editIntent, "Open in...");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { viewIntent });
startActivity(chooserIntent);

UPDATE: Here is good solution to your answer. How to make an intent with multiple actions

Nazarii Moshenskiy
  • 1,802
  • 2
  • 16
  • 35
0

I tried the same approach as on Facebook's official documentation then tested on Huawai P9 Lite, Huawai P20 Lite and on Samsung S8 - it only worked on Samsung S8 for not known reason (to me). I gave up on it since, obviously, it's not working on most of the phones.

zeroDivider
  • 1,050
  • 13
  • 29
0
// Define image asset URI
Uri stickerAssetUri = Uri.parse("your-image-asset-uri-goes-here");
String sourceApplication = "com.my.app";

// Instantiate implicit intent with ADD_TO_STORY action,
// sticker asset, and background colors
Intent intent = new Intent("com.instagram.share.ADD_TO_STORY");
intent.putExtra("source_application", sourceApplication);

intent.setType(MEDIA_TYPE_JPEG);
intent.putExtra("interactive_asset_uri", stickerAssetUri);
intent.putExtra("top_background_color", "#33FF33");
intent.putExtra("bottom_background_color", "#FF00FF");

// Instantiate activity and verify it will resolve implicit intent
Activity activity = getActivity();
activity.grantUriPermission("com.instagram.android", stickerAssetUri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
if (activity.getPackageManager().resolveActivity(intent, 0) != null) {  activity.startActivityForResult(intent, 0);}
svg
  • 114
  • 1
  • 8