31

I managed to open the Twitter and Facebook user profile from my app. But I can not find any references to Instagram.

Is There a way to open Instagram in order to show a user profile like in twitter or facebook?

For instance, in order to get the Intent to launch the twitter application I do:

public Intent getOpenTwitterIntent(Context context) {

    try {
        return new Intent(Intent.ACTION_VIEW,
                Uri.parse("twitter://user?screen_name="
                        .concat(twitterUsername)));

    } catch (Exception e) {
        return new Intent(
                Intent.ACTION_VIEW,
                Uri.parse("https://twitter.com/#!/".concat(twitterUsername)));
    }

}

How can I achive something similar with Instagram? Thanks in advance.

RoCkDevstack
  • 3,517
  • 7
  • 34
  • 56
Blackbelt
  • 156,034
  • 29
  • 297
  • 305

7 Answers7

43

Here is a method to get the Intent to open the Instagram app to the user's profile page:

/**
 * Intent to open the official Instagram app to the user's profile. If the Instagram app is not
 * installed then the Web Browser will be used.</p>
 * 
 * Example usage:</p> {@code newInstagramProfileIntent(context.getPackageManager(), 
 *     "http://instagram.com/jaredrummler");}</p>
 * 
 * @param pm
 *            The {@link PackageManager}. You can find this class through
 *            {@link Context#getPackageManager()}.
 * @param url
 *            The URL to the user's Instagram profile.
 * @return The intent to open the Instagram app to the user's profile.
 */
public static Intent newInstagramProfileIntent(PackageManager pm, String url) {
    final Intent intent = new Intent(Intent.ACTION_VIEW);
    try {
        if (pm.getPackageInfo("com.instagram.android", 0) != null) {
            if (url.endsWith("/")) {
                url = url.substring(0, url.length() - 1);
            }
            final String username = url.substring(url.lastIndexOf("/") + 1);
            // http://stackoverflow.com/questions/21505941/intent-to-open-instagram-user-profile-on-android
            intent.setData(Uri.parse("http://instagram.com/_u/" + username));
            intent.setPackage("com.instagram.android");
            return intent;
        }
    } catch (NameNotFoundException ignored) {
    }
    intent.setData(Uri.parse(url));
    return intent;
}
Jared Rummler
  • 37,824
  • 19
  • 133
  • 148
  • 2
    I'm glad you used the `"http://instagram.com/_u/..."` URI. That worked for me to open a profile in the Instagram app (as long as it was installed) and not the default browser. – AdamMc331 Oct 29 '15 at 20:32
9

So far I couldn't find a way to show user's profile directly.. but there is a way around..

Found this solution from Instagram Manifest from GitHub

 Intent iIntent = getPackageManager().getLaunchIntentForPackage("com.instagram.android");

iIntent.setComponent(new ComponentName( "com.instagram.android", "com.instagram.android.activity.UrlHandlerActivity"));
iIntent.setData( Uri.parse( "http://instagram.com/p/gjfLqSBQTJ/") );

startActivity(iIntent);

This will open particular image post by the user in the app. From the page app user can open the profile with the link inside.

If you want to open recent post or something , you can use Instagram API

This is not we want exactly , but the better option now... i guess :)

Dinesh Balu
  • 125
  • 2
  • 9
  • 8
    @dinesh-balu you solution work just need to change the URL to `iIntent.setData( Uri.parse( "http://instagram.com/p/gjfLqSBQTJ/") );` to `intent.setData( Uri.parse( "http://instagram.com/_u/" + INSTAGRAM_USERNAME) );` **URL** http://instagram.com/_u/ **[USERNAME]** – eOnOe Jul 31 '14 at 23:35
  • It works only if account is public. For example. First take media link, like this http://instagram.com/p/xxxXXxxx/ but from your own public account after that do your account private and try to launch this code. And it will not work. Instead media you will see blank instagram app page. – raiym Jun 10 '15 at 15:23
8

After a brief search and trying what we all know "WON'T WORK" I got this to work

 Uri uri = Uri.parse("http://instagram.com/mzcoco2you");
 Intent intent = new Intent(Intent.ACTION_VIEW, uri);
 startActivity(intent);

This should start your default browser and ... there you go. The answer is "instagram.com" + "/UserName".

KaSiris
  • 407
  • 3
  • 13
  • 3
    I tried it, it opens the browser not the app. Is there a way to open the app? – Blackbelt Jun 26 '13 at 12:38
  • it looks like you can open pictures in the instagram app with an intent, but not (yet) user profiles. details: https://groups.google.com/d/msg/instagram-api-developers/7XUKm9HSAdg/9SrdVmB4trQJ – ryan Jul 30 '13 at 23:50
4

To open directly instagram app to a user profile :

String scheme = "http://instagram.com/_u/USER";
String path = "https://instagram.com/USER";
String nomPackageInfo ="com.instagram.android";
    try {
        activite.getPackageManager().getPackageInfo(nomPackageInfo, 0);
        intentAiguilleur = new Intent(Intent.ACTION_VIEW, Uri.parse(scheme));
        } catch (Exception e) {
            intentAiguilleur = new Intent(Intent.ACTION_VIEW, Uri.parse(path));
        }
        activite.startActivity(intentAiguilleur); 

// Use this link to open directly a picture
  String scheme = "http://instagram.com/_p/PICTURE";
Kamal
  • 161
  • 1
  • 3
2
try {
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setData(Uri.parse("http://instagram.com/_u/" + "username"));
    intent.setPackage("com.instagram.android");
    startActivity(intent);
    } 
  catch (android.content.ActivityNotFoundException anfe) 
     {
       startActivity(new Intent(Intent.ACTION_VIEW,
       Uri.parse("https://www.instagram.com/" + username)));
     }
Meenal
  • 2,879
  • 5
  • 19
  • 43
Ashwin H
  • 695
  • 7
  • 24
0

Unfortunately, at this time you cannot open the Instagram app to go directly to a user profile. You can however open a certain photo in the Instagram app.

The following code that I have provided will open a certain photo inside of the Instagram app. If no Instagram app is installed, it will open a user profile inside of the browser.

public void openInstagram (View view) {
    Intent intent = null;
    String pictureId = "p/0vtNxgqHx9";
    String pageName = "d4v3_r34d";
    try {intent = getPackageManager().getLaunchIntentForPackage("com.instagram.android");
        intent.setComponent(new ComponentName("com.instagram.android", "com.instagram.android.activity.UrlHandlerActivity"));
        intent.setData(Uri.parse("http://instagram.com/" + pictureId));
    } catch (Exception e) {
        intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://instagram.com/" + pageName));
    }
    this.startActivity(intent);

I made it very easy for this code to be edited for your own purposes. Set the "String pictureId" to the id of the picture that you want to display and set the "String pageName" to the user name of your Instagram account.

The user name part is obvious but if you need help to get your picture id look at this picture.

David Read
  • 1,089
  • 1
  • 9
  • 23
0

Check the code below:

Intent instaintent = getPackageManager().getLaunchIntentForPackage("com.instagram.android");
instaintent.setData(Uri.parse("https://www.instagram.com/insta_save_new/"));    
startActivity(instaintent);
piotrek1543
  • 19,130
  • 7
  • 81
  • 94