Skip to content

Instantly share code, notes, and snippets.

@michaeltys
Created October 10, 2018 15:13
Show Gist options
  • Save michaeltys/a8613e5aea9db8e4684bf85568e40160 to your computer and use it in GitHub Desktop.
Save michaeltys/a8613e5aea9db8e4684bf85568e40160 to your computer and use it in GitHub Desktop.
Sharing an image to instagram stories or create a post
private void shareFileToInstagram(Uri uri, boolean isVideo, Post post) {
Intent feedIntent = new Intent(Intent.ACTION_SEND);
feedIntent.setType(isVideo ? "video/*" : "image/*");
feedIntent.putExtra(Intent.EXTRA_STREAM, uri);
feedIntent.setPackage(Constants.INSTAGRAM_PACKAGE_NAME);
Intent storiesIntent = new Intent("com.instagram.share.ADD_TO_STORY");
storiesIntent.setDataAndType(uri, isVideo ? "mp4" : "jpg");
storiesIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
storiesIntent.setPackage(Constants.INSTAGRAM_PACKAGE_NAME);
activity.grantUriPermission(
"com.instagram.android", uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
Intent chooserIntent = Intent.createChooser(feedIntent, getString(R.string.social_instagram));
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] {storiesIntent});
startActivity(chooserIntent);
}
@gokmenbayram
Copy link

You can try this. It worked for me. Directly Instagram story.

    private void shareInstagram(Uri uri) {
        Intent feedIntent = new Intent(Intent.ACTION_SEND);
        feedIntent.setType("image/*");
        feedIntent.putExtra(Intent.EXTRA_STREAM, uri);
        feedIntent.setPackage(Contracts.INSTAGRAM_PACKAGE_NAME);

        Intent storiesIntent = new Intent(Contracts.INSTAGRAM_STORY_ACTION_NAME);
        storiesIntent.setDataAndType(uri, "image/*");
        storiesIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        storiesIntent.setPackage(Contracts.INSTAGRAM_PACKAGE_NAME);

        this.grantUriPermission(
                Contracts.INSTAGRAM_PACKAGE_NAME, uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);

        feedIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[]{storiesIntent});
        startActivity(feedIntent);
    }

@bhumikash-tech
Copy link

Hey @gokmenbayram ........could you please tell me what you actually passed when you call this method ????

@bhumikash-tech
Copy link

@michaeltys, could you please tell me where and how you call your method?? What you pass on the call of this method????

@gokmenbayram
Copy link

Hey @gokmenbayram ........could you please tell me what you actually passed when you call this method ????

i didnt understand this comment.

@bhumikash-tech
Copy link

Hey @gokmenbayram ...i meant that I understood your shareInstagram(Uri uri method ) but I want to know what you actually passed as uri while calling this method???? Please do let me know..

@gokmenbayram
Copy link

with provider.

public Uri getImageUri() {
File directory = new File(Files.statisticsSSPath());
File file = new File(directory, 0 + ".png");
return FileProvider.getUriForFile(Application.context(), Application.context().getPackageName() +".provider",file);
}

@AndroidDeveloperMCT
Copy link

Is there a way to get a callback that post/story shared successfully?

@adnandothussain
Copy link

Is there any way around for react native/expo ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment