'Intent to open Instagram user profile on Android

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?



Solution 1:[1]

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")));
}

Solution 2:[2]

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;
}

Solution 3:[3]

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";

Solution 4:[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);

            }
        });

Solution 5:[5]

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)
    }
}

Solution 6:[6]

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)

            }

Solution 7:[7]

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")));
    }

}

Solution 8:[8]

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/

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 juanjo
Solution 2 Rahul Sainani
Solution 3 Kamal
Solution 4 Aditya Vyas-Lakhan
Solution 5 Kaaveh Mohamedi
Solution 6 Alex Karapanos
Solution 7 JoSSte
Solution 8 Encoder's YT