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);
}
@PasqualePuzio
Copy link

Thank you. It really helped 🥇

@mahendrakumarpal
Copy link

Through above code while we sharing images or video we getting two option that are feed and stories.I have to open stories directly rather than two option like above code shows.Can we open stories directly.

@mahendrakumarpal
Copy link

mahendrakumarpal commented Jun 27, 2019

Hello PasqualePuzio,

Thank you for sharing the post.

We need your help to directly post the story to Instagram on click of share, but we use the above code we are able to see a screen inbetween which asks the user to select the type of post(Instagram Feed/Instagram Story) we dont need this option. Request to help us and share the details to achieve this.

Below are the details

Source code:

           Uri backgroundAssetUri = FileProvider.getUriForFile(context, "com.example.provider", imagePath);
          
            Intent feedIntent = new Intent(Intent.ACTION_SEND);
            feedIntent.setType("image/*");
            feedIntent.putExtra(Intent.EXTRA_STREAM, backgroundAssetUri);
            feedIntent.setPackage(Utils.INSTAGRAM_PACKAGE_NAME);

            Intent storiesIntent = new Intent("com.instagram.share.ADD_TO_STORY");
            storiesIntent.setDataAndType(backgroundAssetUri,  "jpg");
            storiesIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            storiesIntent.setPackage(Utils.INSTAGRAM_PACKAGE_NAME);
            context.grantUriPermission(
                    Utils.INSTAGRAM_PACKAGE_NAME, backgroundAssetUri, Intent.FLAG_GRANT_READ_URI_PERMISSION);

            Intent chooserIntent = Intent.createChooser(feedIntent, context.getString(R.string.social_instagram));
            chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] {storiesIntent});
            context.startActivity(chooserIntent);

Its working as I expected. I want to open Screen3 directly after clicked on "Share with instagram" from screen1. I dont want to show screen2.
Please guide me how can I achieve this.

screen1
screen2
screen3

@Eldhopj
Copy link

Eldhopj commented Mar 19, 2020

Hi

Hello PasqualePuzio,

Thank you for sharing the post.

We need your help to directly post the story to Instagram on click of share, but we use the above code we are able to see a screen inbetween which asks the user to select the type of post(Instagram Feed/Instagram Story) we dont need this option. Request to help us and share the details to achieve this.

Below are the details

Source code:

           Uri backgroundAssetUri = FileProvider.getUriForFile(context, "com.example.provider", imagePath);
          
            Intent feedIntent = new Intent(Intent.ACTION_SEND);
            feedIntent.setType("image/*");
            feedIntent.putExtra(Intent.EXTRA_STREAM, backgroundAssetUri);
            feedIntent.setPackage(Utils.INSTAGRAM_PACKAGE_NAME);

            Intent storiesIntent = new Intent("com.instagram.share.ADD_TO_STORY");
            storiesIntent.setDataAndType(backgroundAssetUri,  "jpg");
            storiesIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            storiesIntent.setPackage(Utils.INSTAGRAM_PACKAGE_NAME);
            context.grantUriPermission(
                    Utils.INSTAGRAM_PACKAGE_NAME, backgroundAssetUri, Intent.FLAG_GRANT_READ_URI_PERMISSION);

            Intent chooserIntent = Intent.createChooser(feedIntent, context.getString(R.string.social_instagram));
            chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] {storiesIntent});
            context.startActivity(chooserIntent);

Its working as I expected. I want to open Screen3 directly after clicked on "Share with instagram" from screen1. I dont want to show screen2.
Please guide me how can I achieve this.

screen1
screen2
screen3

I too want to get the same I researched it a bit but no luck.
If u happen to crack the issue, could you please share the code with us

@michaeltys
Copy link
Author

Simply call startActivity(feedIntent) or with other one intent from the example. But keep in mind to check if Instagram is installed before starting this intent to avoid fatal exception being thrown

@NALAndrod2
Copy link

Hi @michaeltys, I got a problem when sharing a photo on Instagram.
I using FileProvider.getUriForFile, but this only works when sharing "Direct" or "Story". When sharing on Feed, Instagram app got an error and don't file my file.
Could you please help me, please

@ysnklcky
Copy link

Hi @michaeltys, i had same issue with @NALAndrod2

@NALAndrod2 did you find a solution ? I have been searching for this 5 days.

@NALAndrod2
Copy link

@ysnklcky, Not yet :(. It works normally on android below N with Uri.parse(file.absolutePath). I'm still I am still researching. The document of Instagram developer seems to be too old

@ysnklcky
Copy link

ysnklcky commented Mar 24, 2020

@NALAndrod2
Copy link

@mauriblint
Copy link

Hello guys, any news? I trying to fix this issue from the last week :/

@mauriblint
Copy link

Workaround solution to fix temporary this IG bug (I think is a bug from the Instagram app)
https://stackoverflow.com/questions/60776730/instagram-android-share-intent-unable-to-load-image

@Pranay220
Copy link

Hello,I want to click a picture and post it on instagram using app I have made + I want to include Hastags

@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