Intent to open Instagram user profile on 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")));
}
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";
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;
}