How to open a specific album or photo in facebook

2019-08-15 08:16发布

问题:

How to open an album or photo in facebook app using intent from your own Android App?

I been searching for the specific answer for this one. Most question are about how to open a facebook page via intent.

I saw this one (regards to Sunny a Sr. Software engineer at iAppStreet) but it doesn't work.

public Intent getOpenFacebookIntent(String pId) {

try {
    activity.getPackageManager().getPackageInfo("com.facebook.katana", 0);
    return new Intent(Intent.ACTION_VIEW, Uri.parse("facebook:/photos?album=0&photo=" + pId+"&user="+ownerId));
} catch (Exception e) {
    return new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/"));
}
}

startActivity(getOpenFacebookIntent(pid));

thanks.

回答1:

Actually i made this question to help those who have the same problem like me. As of today (28 March 2016) i just found out how to open an album using intent but i cannot open a photo using intent in the facebook app. I am a beginner in android programming so please bear with me.

I used a button to implement this. Here is the code:

Button buttonOpenALbum = (Button) findViewById(R.id.button);
buttonOpenALbum.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            //It is a album from a facebook page @ www.facebook.com/thinkingarmy
            String albumID = "575146485903630";
            String userID = "575145312570414";

            String url = "facebook:/photos?album="+albumID+"&user="+userID;
            Uri uri = Uri.parse(url);
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(intent);
        }
    });

If you want to get the albumID and userID of a facebook album try searching on google how or try this one if it helps, https://www.youtube.com/watch?v=sMEmlpmCHLc

Modifying the code to open a specific photo has not produce the expected result. Even though I included the photoID, you still end up in the album. Here is the code:

Button buttonOpenPhoto = (Button) findViewById(R.id.button);
buttonOpenPhoto.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            //It is a album from a facebook page @ www.facebook.com/thinkingarmy
            String albumID = "575146485903630";
            String userID = "575145312570414";
            String photoID = "803154029769540";

            String url = "facebook:/photos?album="+albumID+"&photo="+photoID+"&user="+userID;
            Uri uri = Uri.parse(url);
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(intent);
        }
    });

I can't be sure why. I tried looking but some say the urls of facebook is undocumented. If I may, I think you cannot open a specific photo because upon observation, facebook open a photo in full screen, meaning it is a different Activity than the Main Activity which handles the incoming Intent. The Main Activity of the facebook app only handles the intent up to opening a specific album.

I hope I can help other "new android coders" with my first post.

NB: I just learn coding using google and stackoverflow. It's my way of giving back. Thanks.



回答2:

Launch a view intent with the following URL

https://www.facebook.com/photo.php?fbid={photo_id}

          Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            ctx.startActivity(browserIntent);

If the app is installed, it will prompt the user to open in app, otherwise it will use the browser



回答3:

Some useful information at Open Facebook Page in Facebook App (if installed) on Android

In particular, new versions of facebook app work with

Uri.parse("fb://facewebmodal/f?href=" + FACEBOOKURL

I found this the simplest approach to acheive what was needed.

Note also that to get the URL you can go to the facebook album (on facebook desktop), click the three dots next to "Edit" and "Tag" and select "Get Link". I found this more reliable than simply copying the URL from the browser.