web-dev-qa-db-fra.com

Ouvrir le profil utilisateur Instagram

J'ai réussi à ouvrir les profils d'utilisateur Twitter et Facebook à partir de mon application. Mais je ne trouve aucune référence à Instagram.

Y at-il un moyen d'ouvrir Instagram afin d'afficher un profil d'utilisateur comme dans Twitter ou Facebook?

Par exemple, pour que la Intent lance l'application Twitter, je fais:

public Intent getOpenTwitterIntent(Context context) {

    try {
        return new Intent(Intent.ACTION_VIEW,
                Uri.parse("Twitter://user?screen_name="
                        .concat(twitterUsername)));

    } catch (Exception e) {
        return new Intent(
                Intent.ACTION_VIEW,
                Uri.parse("https://Twitter.com/#!/".concat(twitterUsername)));
    }

}

Comment puis-je obtenir quelque chose de similaire avec Instagram? Merci d'avance.

25
Blackbelt

Voici une méthode permettant à la Intent d'ouvrir l'application Instagram sur la page de profil de l'utilisateur:

/**
 * Intent to open the official Instagram app to the user's profile. If the Instagram app is not
 * installed then the Web Browser will be used.</p>
 * 
 * Example usage:</p> {@code newInstagramProfileIntent(context.getPackageManager(), 
 *     "http://instagram.com/jaredrummler");}</p>
 * 
 * @param pm
 *            The {@link PackageManager}. You can find this class through
 *            {@link Context#getPackageManager()}.
 * @param url
 *            The URL to the user's Instagram profile.
 * @return The intent to open the Instagram app to the user's profile.
 */
public static Intent newInstagramProfileIntent(PackageManager pm, String url) {
    final Intent intent = new Intent(Intent.ACTION_VIEW);
    try {
        if (pm.getPackageInfo("com.instagram.Android", 0) != null) {
            if (url.endsWith("/")) {
                url = url.substring(0, url.length() - 1);
            }
            final String username = url.substring(url.lastIndexOf("/") + 1);
            // http://stackoverflow.com/questions/21505941/intent-to-open-instagram-user-profile-on-Android
            intent.setData(Uri.parse("http://instagram.com/_u/" + username));
            intent.setPackage("com.instagram.Android");
            return intent;
        }
    } catch (NameNotFoundException ignored) {
    }
    intent.setData(Uri.parse(url));
    return intent;
}
35
Jared Rummler

Jusqu'à présent, je ne pouvais pas trouver un moyen d'afficher directement le profil de l'utilisateur .. mais il y a un moyen de le faire .. 

Trouvé cette solution de Manifeste Instagram de GitHub

 Intent iIntent = getPackageManager().getLaunchIntentForPackage("com.instagram.Android");

iIntent.setComponent(new ComponentName( "com.instagram.Android", "com.instagram.Android.activity.UrlHandlerActivity"));
iIntent.setData( Uri.parse( "http://instagram.com/p/gjfLqSBQTJ/") );

startActivity(iIntent);

Cela ouvrira une image particulière de l'utilisateur dans l'application. Depuis la page, l'utilisateur de l'application peut ouvrir le profil avec le lien à l'intérieur.

Si vous voulez ouvrir un article récent ou quelque chose du genre, vous pouvez utiliser Instagram API

Ce n'est pas ce que nous voulons exactement, mais la meilleure option maintenant ... je suppose :)

9
Dinesh Balu

Après une brève recherche et en essayant ce que nous savons tous, "ça ne marchera pas"

 Uri uri = Uri.parse("http://instagram.com/mzcoco2you");
 Intent intent = new Intent(Intent.ACTION_VIEW, uri);
 startActivity(intent);

Cela devrait démarrer votre navigateur par défaut et ... voilà. La réponse est "instagram.com" + "/ Nom d'utilisateur".

8
KaSiris

Pour ouvrir directement l'application instagram à un profil d'utilisateur:

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";
4
Kamal
try {
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setData(Uri.parse("http://instagram.com/_u/" + "username"));
    intent.setPackage("com.instagram.Android");
    startActivity(intent);
    } 
  catch (Android.content.ActivityNotFoundException anfe) 
     {
       startActivity(new Intent(Intent.ACTION_VIEW,
       Uri.parse("https://www.instagram.com/" + username)));
     }
1
Ashwin H

Malheureusement, vous ne pouvez pas ouvrir l'application Instagram pour accéder directement à un profil d'utilisateur. Vous pouvez cependant ouvrir une certaine photo dans l'application Instagram.

Le code suivant que j'ai fourni ouvrira une certaine photo à l'intérieur de l'application Instagram. Si aucune application Instagram n'est installée, un profil d'utilisateur sera ouvert dans le navigateur.

public void openInstagram (View view) {
    Intent intent = null;
    String pictureId = "p/0vtNxgqHx9";
    String pageName = "d4v3_r34d";
    try {intent = getPackageManager().getLaunchIntentForPackage("com.instagram.Android");
        intent.setComponent(new ComponentName("com.instagram.Android", "com.instagram.Android.activity.UrlHandlerActivity"));
        intent.setData(Uri.parse("http://instagram.com/" + pictureId));
    } catch (Exception e) {
        intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://instagram.com/" + pageName));
    }
    this.startActivity(intent);

J'ai rendu très facile la modification de ce code pour vos propres besoins. Définissez "String pictureId" sur l'id de l'image que vous souhaitez afficher et définissez "String pageName" sur le nom d'utilisateur de votre compte Instagram.

La partie relative au nom d’utilisateur est évidente, mais si vous avez besoin d’aide pour obtenir votre identifiant, regardez cette image .

0
David Read