Intent to open Instagram user profile on Android

AndroidAndroid IntentInstagram

Android Problem Overview


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 https://www.instagram.com/developer/mobile-sharing/iphone-hooks/">page</a> 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?

Android Solutions


Solution 1 - Android

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 - Android

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 - Android

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 - Android

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 - Android

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 - Android

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 - Android

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 - Android

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/

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGrishkaView Question on Stackoverflow
Solution 1 - AndroidjhondgeView Answer on Stackoverflow
Solution 2 - AndroidRahul SainaniView Answer on Stackoverflow
Solution 3 - AndroidKamalView Answer on Stackoverflow
Solution 4 - AndroidAditya Vyas-LakhanView Answer on Stackoverflow
Solution 5 - AndroidKaaveh MohamediView Answer on Stackoverflow
Solution 6 - AndroidAlex KarapanosView Answer on Stackoverflow
Solution 7 - AndroidthinktechtvView Answer on Stackoverflow
Solution 8 - AndroidEncoder's YTView Answer on Stackoverflow