Shareing image to Instagram using Expo

Hey guys,
I’m struggling to find a way to share an image(saved on disk or through URL) to Instagram.

For IOS, the solution can be found at

How do I share the image to Instagram?

I tried to use IntentLauncherAndroid. but no luck,

IntentLauncherAndroid.startActivityAsync("android.intent.action.SEND", { uri: "${localpath}" }, "com.instagram.android");

I get the following warning with the above line
[Unhandled promise rejection: Error: No Activity found to handle Intent { act=android.intent.action.SEND dat=com.instagram.android (has extras) }]

Also, through Linking, I can open the Instagram app by calling (But not share image)

Linking.openURL('http:/instagram.com/_u/#Intent;package=com.instagram.android;scheme=https;end');

The Native Solution for this is

React Native Plugins available (I have not tried it as it is not expo)

1 Like

x-post: Make it possible to share an image to Instagram on iOS and Android | Voters | Expo

IntentLauncherAndroid is missing the ability to call setType and setPackage.

On Android the API would look like this:

Intent intent = getPackageManager().getLaunchIntentForPackage("com.instagram.android");
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setPackage("com.instagram.android");
try {
  shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(MediaStore.Images.Media.insertImage(getContentResolver(), imagePath, "I am Happy", "Share happy !")));
} catch (FileNotFoundException e) {
}
shareIntent.setType("image/jpeg");
startActivity(shareIntent);     

If you want to open a PR I can work through it with you. Not sure the best solution for this given that ReactNative.Share & ReactNative.Linking both drop the ball on this.
It would be nice to unify the process across platforms rather than to use IntentLauncherAndroid .

1 Like

Yea, that would be a great idea.
Eagerly waiting for a solution.

I could provide whatever help you need for the PR

I am from the same team as @ragularuban I wrote the below-overloaded method to support this. I have patched the class IntentLauncherModule.

@ReactMethod
  public void startActivity(String activity, @Nullable ReadableMap data, Promise promise, String type, String packageName) {
    if (pendingPromise != null) {
      pendingPromise.reject("ERR_INTERRUPTED", "A new activity was started");
      pendingPromise = null;
    }

    if (activity == null || activity.isEmpty()) {
      promise.reject("ERR_EMPTY_ACTIVITY", "Specified activity was empty");
      return;
    }

    try {
      Activity currentActivity = getCurrentActivity();
      Intent intent = new Intent(activity);

      if (data != null) {
        intent.putExtras(Arguments.toBundle(data));
      }

      if(packageName != null){
          intent.setPackage(packageName);
      }
      if(type != null){
          intent.setType(type);
      }

      if (currentActivity != null) {
        currentActivity.startActivity(intent);
      } else {
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        getReactApplicationContext().startActivity(intent);
      }

      pendingPromise = promise;
    } catch (Exception e) {
      promise.reject("ERR_LAUNCHING_ACTIVITY", e);
    }
  }

  @ReactMethod
  public void startActivity(String activity, @Nullable ReadableMap data, Promise promise) {
    startActivity(activity, data, promise, null, null);
  }

Shall I submit a PR for this? Also if the PR gets merged - do we have to wait till the next release comes out? Or can we use the master branch for our builds?

Cheers~

1 Like

You should definitely make a PR and ask for this to get merged.

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.