65

I'm developing a social networking app and our users can connect their Instagram account to our service. I'd like to open Instagram profiles directly in their official Android app (if it is installed) but I can't find any way to do that. However, there is a page on their developer site about the exact same feature on iOS but this doesn't seem to work on Android at all. Everything I found on the web only suggests various ways to open links in a browser. Any suggestions?

Grishka
  • 2,465
  • 1
  • 20
  • 38
  • @kentarosu yes, but there is no correct answer. – Grishka Feb 02 '14 at 10:38
  • Read the comments of the answer chosen as correct. It says you can't open the IG app at this point to a profile, only to a picture. The closest solution is to open the browser. – gatlingxyz Feb 02 '14 at 15:48

8 Answers8

186

I solved this problem using the following code.

Uri uri = Uri.parse("http://instagram.com/_u/xxx");
Intent likeIng = new Intent(Intent.ACTION_VIEW, uri);

likeIng.setPackage("com.instagram.android");

try {
    startActivity(likeIng);
} catch (ActivityNotFoundException e) {
    startActivity(new Intent(Intent.ACTION_VIEW, 
        Uri.parse("http://instagram.com/xxx")));
}
juanjo
  • 3,737
  • 3
  • 39
  • 44
jhondge
  • 2,352
  • 1
  • 17
  • 13
34

Although @jhondge's solution works and is correct. This is a more cleaner way to do this:

Uri uri = Uri.parse("http://instagram.com/_u/xxx");
    Intent insta = new Intent(Intent.ACTION_VIEW, uri);
    insta.setPackage("com.instagram.android");

    if (isIntentAvailable(mContext, insta)){
        startActivity(insta);
    } else{
        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://instagram.com/xxx")));
    }

private boolean isIntentAvailable(Context ctx, Intent intent) {
    final PackageManager packageManager = ctx.getPackageManager();
    List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
    return list.size() > 0;
}
Rahul Sainani
  • 3,437
  • 1
  • 34
  • 48
  • I like this solution, but for some reason it does not work for me when I try to do the exact same thing for a Facebook Intent. The try/catch approach works for Facebook though, and your approach is fine for an Instagram Intent. Do you by chance know what could cause this? – Krøllebølle Feb 05 '16 at 17:16
  • @Krøllebølle I think I faced the same issue and switched to try catch for facebook. Don't know what causes this. – Rahul Sainani Feb 09 '16 at 16:51
  • This works for me... I Tried it in Android Emulator without Instagram app it opened in the browser and I tried it in my Mobile in which Instagram is installed and it opened the Instagram app. Thank You. – Ramesh Aug 16 '19 at 21:24
12

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
4

I tried this way and it worked for me..

instabtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


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

                instaintent.setComponent(new ComponentName( "com.instagram.android", "com.instagram.android.activity.UrlHandlerActivity"));
                instaintent.setData( Uri.parse( "https://www.instagram.com/_u/bitter_truth_lol") );

                startActivity(instaintent);

            }
        });
Aditya Vyas-Lakhan
  • 13,409
  • 16
  • 61
  • 96
4

Based on @alex-karapanos answer, I use this code:

fun launchInsta() {
    val uriForApp: Uri = Uri.parse("http://instagram.com/_u/xxx")
    val forApp = Intent(Intent.ACTION_VIEW, uriForApp)

    val uriForBrowser: Uri = Uri.parse("http://instagram.com/xxx")
    val forBrowser = Intent(Intent.ACTION_VIEW, uriForBrowser)

    forApp.component =
        ComponentName(
            "com.instagram.android",
            "com.instagram.android.activity.UrlHandlerActivity"
        )

    try {
        startActivity(forApp)
    } catch (e: ActivityNotFoundException) {
        startActivity(forBrowser)
    }
}
Kaaveh Mohamedi
  • 1,399
  • 1
  • 15
  • 36
2

Kotlin Version of @jhondge answer:

            val uriForApp: Uri = Uri.parse("http://instagram.com/_u/xxx")
            val forApp = Intent(Intent.ACTION_VIEW, uriForApp)

            val uriForBrowser: Uri = Uri.parse("http://instagram.com/xxx")
            val forBrowser = Intent(Intent.ACTION_VIEW, uriForBrowser)

            forApp("com.instagram.android")

            try {
                startActivity(context, forApp, null)

            } catch (e: ActivityNotFoundException) {
                startActivity(context, forBrowser, null)

            }
Alex Karapanos
  • 895
  • 2
  • 7
  • 20
1

I implemented this using fragment in webview but I have one issue, the instagram pop up comes three times :

webView.setWebViewClient(new WebViewClient()
        {
 public boolean shouldOverrideUrlLoading(WebView viewx, String urlx)
            {
 if(Uri.parse(urlx).getHost().endsWith("instagram.com")) {

                    gotoinstagram();

                  return false;
                }

                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(urlx));
                viewx.getContext().startActivity(intent);
                return true;
            }



        });

outside of onCreateView

//instagram

public void gotoinstagram()
{

    Uri uri = Uri.parse("http://instagram.com/_u/XXXX");
    Intent likeIng = new Intent(Intent.ACTION_VIEW, uri);

    likeIng.setPackage("com.instagram.android");

    try {
        startActivity(likeIng);
    } catch (ActivityNotFoundException e) {
        startActivity(new Intent(Intent.ACTION_VIEW,
                Uri.parse("http://instagram.com/XXXX")));
    }

}
JoSSte
  • 2,953
  • 6
  • 34
  • 54
1

A number of people already answered this question but still I am answering. Using the method answered above the workflow is go through 3 steps.

Step1: The app parse uri and intent

Step2: The intent go to browser and load Instagram url

Step3: Browser then redirect to Instagram App

But to open Instagram app directly without going to Browser you can use the method mentioned below. Create an Intent method like below:-

private Intent instaIntn(Context context) {
Intent i1;
String instaId = "your insta id";
String appResolver = "instagram://user?username=";
String webResolver = "https://instagram.com/";
String instaPackageName = "com.instagram.android";
String instaLitePackName = "com.instagram.lite";

try {
context.getPackageManager().getPackageInfo(instaPackageName, 0);
i1 = new Intent(Intent.ACTION_VIEW, Uri.parse(appResolver+instaId));
} catch (PackageManager.NameNotFoundException e1) {
Toast.makeText(getApplication(), "Instagram not found", Toast.LENGTH_SHORT).show();
try {
context.getPackageManager().getPackageInfo(instaLitePackName, 0);
i1 = new Intent(Intent.ACTION_VIEW, Uri.parse(appResolver+instaId));
} catch (PackageManager.NameNotFoundException e2) {
Toast.makeText(getApplication(), "Instagram and instagram lite not found", Toast.LENGTH_SHORT).show();
i1 = new Intent(Intent.ACTION_VIEW, Uri.parse(webResolver+instaId));
}
}
return i1;
}

Now you can Start this intent from anywhere inside class by simply calling below statement:-

startActivity(instaIntn(getApplicationContext()));

Now what it will do, it will try to open Instagram app, if instagram app is not installed then it will try to open Instagram lite. If both app is missing then it will intent to browser. The above method intent user to Instagram app and user profile. You can also intent user to Instagram image video and other page. See full documentation in here https://developers.facebook.com/docs/instagram/sharing-to-feed/