react-native-share: Share on instagram story doesn't work on Android

In my React Native app I’m using the following code to share content on instagram stories. On iOS, it works fine; however, on Android, it does not resolve the promise and does not display any error.

import { default as RNShare } from "react-native-share";

const shareOnInstagram = async (callback) => {
  const imageUrl =
    "https://images.unsplash.com/photo-1556035511-3168381ea4d4?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1674&q=80";

  /* Download image using expo-file-system */
  const base64 = await downloadImage({ url: imageUrl, isBase64: true });

  const shareOptions = {
    backgroundImage: imageUrl,
    stickerImage: "data:image/png;base64," + base64,
    backgroundBottomColor: "#fefefe",
    backgroundTopColor: "#906df4",
    social: RNShare.Social.INSTAGRAM_STORIES,
    appId: META_API_KEY,
  };

  RNShare.shareSingle(shareOptions).catch((err) => {
    console.log(err);
  });

  callback();
};

Here’s the specs:

"react-native": "^0.70.6"
"react-native-share": "^8.0.1"

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 26 (5 by maintainers)

Most upvoted comments

Add this queries into your androidManifset,xml file

 <queries>
  <package android:name="com.google.android.youtube" />
  <package android:name="com.facebook.katana" />
  <package android:name="com.facebook.orca" />
  <package android:name="com.facebook.mlite" />
  <package android:name="com.facebook.lite" />
  <package android:name="org.telegram.messenger" />
  <package android:name="com.whatsapp" />
  <package android:name="com.instagram.android" />
</queries>

same issue for me : when I try to share on instagram, it open the photo library on Android.

@adnandothussain I actually wrote a blogpost explaining the entire process, you can find the blog here.

I was actually getting the error message: Value for backgroundImage cannot be cast from Double to String And doing this solved it for me:

Step 1: Converted the image to base64 using some online tool and stored it as a variable

export const bgImage =
  "data:image/png;base64," + "iVBORw0KGgoAAAANSUhEUgAAAtAAAAUACAYAAABuzmU9AAAACXBIWXMAAAsTAAALEwEAmpwYAAAAAXNSR0IArs4c6QAA ................"

Step 2: pass this bgImage variable to react-native-share’s backgroundImage

  await Share.shareSingle({
          appId: "", // NOTE: replace this app Id from your facebook developer account
          stickerImage: uri,

          social: Share.Social.INSTAGRAM_STORIES,
          backgroundBottomColor: "#1D1D1D",
          backgroundTopColor: "#1D1D1D",

          backgroundImage: bgImage, // <-- Important part
        });

@Svarto I’m using an expo managed app, anything for that?

I assume you are also using EAS build. Create a custom plugin, with something like this (this is just want I’m using, fixes it for Facebook, twitter and Instagram):

const { 
   AndroidConfig, 
   ConfigPlugin, 
   withAndroidManifest, 
 } = require("expo/config-plugins"); 
  
 // Use these imports in SDK 46 and lower 
 // import { AndroidConfig, ConfigPlugin, withAndroidManifest } from '@expo/config-plugins'; 
 // import { ExpoConfig } from '@expo/config-types'; 
  
 // Using helpers keeps error messages unified and helps cut down on XML format changes. 
 const { 
   addMetaDataItemToMainApplication, 
   getMainApplicationOrThrow, 
   writeAndroidManifestAsync, 
 } = AndroidConfig.Manifest; 
  
 // Write in the apps we want to shareSingle to inside the Android manifest 
 // to give them visibility after privacy updates to Android 12 
 module.exports = config => { 
   return withAndroidManifest(config, async config => { 
     config.modResults = await setCustomConfigAsync(config, config.modResults); 
     return config; 
   }); 
 }; 
  
 // Splitting this function out of the mod makes it easier to test. 
 async function setCustomConfigAsync(config, androidManifest) { 
   androidManifest.manifest.queries[0].package = [ 
     { $: { "android:name": "com.instagram.android" } }, 
     { $: { "android:name": "com.twitter.android" } }, 
     { $: { "android:name": "com.facebook.katana" } }, 
   ]; 
  
   return androidManifest; 
 }