Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 584
Grishka Avatar asked Feb 02 '14 01:02

Grishka


2 Answers

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"))); } 
like image 57
jhondge Avatar answered Oct 18 '22 16:10

jhondge


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; } 
like image 30
Rahul Sainani Avatar answered Oct 18 '22 16:10

Rahul Sainani