Skip to content

Commit

Permalink
fix(android): show extra dialog on Android, when users share a video …
Browse files Browse the repository at this point in the history
…to instagram (#1277)

* Reproduce forceDialog being ignored when sharing a video to instagram on Android

* implement sharing a video to instagram with additional dialog, where user can choose between sharing options

* code cleanup

* reduce code duplication

* support also other type of images and videos besides tha jpeg and mp4 format

* added send callback after sharing to instagram
  • Loading branch information
KristineTrona authored Oct 11, 2022
1 parent ce90026 commit acd283e
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 9 deletions.
31 changes: 22 additions & 9 deletions android/src/main/java/cl/json/social/InstagramShare.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,10 @@ public void open(ReadableMap options) throws ActivityNotFoundException {
return;
}
String type = options.getString("type");
String extension = this.getExtension(type);
Boolean isImage = type.startsWith("image");

if (isImage) {
this.openInstagramIntentChooserForImage(url, chooserTitle);
} else {
super.openIntentChooser();
}
this.openInstagramIntentChooser(url, chooserTitle, isImage, extension);
}

protected void openInstagramUrlScheme(String url) {
Expand All @@ -61,18 +58,33 @@ protected void openInstagramUrlScheme(String url) {
super.openIntentChooser();
}

protected void openInstagramIntentChooserForImage(String url, String chooserTitle) {
private String getExtension(String url) {
String[] ext = url.split("/");
return ext[ext.length -1];
}

protected void openInstagramIntentChooser(String url, String chooserTitle, Boolean isImage, String extension) {
Boolean shouldUseInternalStorage = ShareIntent.hasValidKey("useInternalStorage", options) && options.getBoolean("useInternalStorage");
ShareFile shareFile = new ShareFile(url, "image/jpeg", "image", shouldUseInternalStorage, this.reactContext);
ShareFile shareFile = isImage
? new ShareFile(url, "image/" + extension, "image", shouldUseInternalStorage, this.reactContext)
: new ShareFile(url, "video/" + extension, "video", shouldUseInternalStorage, this.reactContext);
Uri uri = shareFile.getURI();

Intent feedIntent = new Intent(Intent.ACTION_SEND);
feedIntent.setType("image/*");

if (isImage) {
feedIntent.setType("image/*");
} else {
feedIntent.setType("video/*");
}

feedIntent.putExtra(Intent.EXTRA_STREAM, uri);
feedIntent.setPackage(PACKAGE);

Intent storiesIntent = new Intent("com.instagram.share.ADD_TO_STORY");
storiesIntent.setDataAndType(uri, "jpg");

storiesIntent.setDataAndType(uri, extension);

storiesIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
storiesIntent.setPackage(PACKAGE);

Expand All @@ -83,6 +95,7 @@ protected void openInstagramIntentChooserForImage(String url, String chooserTitl
Activity activity = this.reactContext.getCurrentActivity();
activity.grantUriPermission(PACKAGE, uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
this.reactContext.startActivity(chooserIntent);
TargetChosenReceiver.sendCallback(true, true, this.getIntent().getPackage());
}

@Override
Expand Down
41 changes: 41 additions & 0 deletions example/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import Share from 'react-native-share';

import images from './images/imagesBase64';
import pdfBase64 from './images/pdfBase64';
import {video} from './videos/videoBase64';

const App = () => {
const [packageSearch, setPackageSearch] = useState<string>('');
Expand Down Expand Up @@ -163,6 +164,40 @@ const App = () => {
}
};

const shareVideoToInstagram = async () => {
const shareOptions = {
title: 'Share video to instagram',
type: 'video/mp4',
url: video,
social: Share.Social.INSTAGRAM,
};

try {
const ShareResponse = await Share.shareSingle(shareOptions);
setResult(JSON.stringify(ShareResponse, null, 2));
} catch (error) {
console.log('Error =>', error);
setResult('error: '.concat(getErrorString(error)));
}
};

const shareImageToInstagram = async () => {
const shareOptions = {
title: 'Share image to instagram',
type: 'image/jpeg',
url: images.image1,
social: Share.Social.INSTAGRAM,
};

try {
const ShareResponse = await Share.shareSingle(shareOptions);
setResult(JSON.stringify(ShareResponse, null, 2));
} catch (error) {
console.log('Error =>', error);
setResult('error: '.concat(getErrorString(error)));
}
};

const shareToInstagramDirect = async () => {
const shareOptions = {
message: encodeURI('Checkout the great search engine: https://google.com'),
Expand Down Expand Up @@ -311,6 +346,12 @@ const App = () => {
<View style={styles.button}>
<Button onPress={shareEmailImage} title="Share Social: Email" />
</View>
<View style={styles.button}>
<Button onPress={shareVideoToInstagram} title="Share Video to IG" />
</View>
<View style={styles.button}>
<Button onPress={shareImageToInstagram} title="Share Image to IG" />
</View>
<View style={styles.button}>
<Button onPress={shareToInstagramStory} title="Share to IG Story" />
</View>
Expand Down
4 changes: 4 additions & 0 deletions example/videos/videoBase64.js

Large diffs are not rendered by default.

0 comments on commit acd283e

Please sign in to comment.