Miu Octavian
Miu Octavian

Reputation: 95

Share link and image on facebook

I have this simple problem made complicated because of FB. I try to share from android a link and image using facebook sdk. Did anyone played with ShareOpenGraphObject, ShareOpenGraphAction and ShareOpenGraphContent before, facebook documentation just sucks, no examples at all. I am waiting for examples.

Thanks

Upvotes: 4

Views: 4028

Answers (4)

JItesh SUvarna
JItesh SUvarna

Reputation: 663

For sharing link and image , ShareDialog Provides functionality to share content via the Facebook Share Dialog and

ShareLinkContent Describes link content to be shared.

is one method without the Open Graph method.

Snippet is like

private ShareDialog shareDialog;
private boolean canPresentShareDialogWith;

shareDialog = new ShareDialog(this);

canPresentShareDialogWith = ShareDialog.canShow(ShareLinkContent.class);


ShareLinkContent linkContent = new ShareLinkContent.Builder().setContentTitle("Shared from " + "<APP NAME>")
                            .setContentDescription(
                                    "Question:" + data.getQuestion() + "\n"
                            // + "Asked by : "
                            // + data.getName() + "\n"
                            )
                            .setContentUrl(
                                    Uri.parse("<Website url>"))
                            .setImageUrl(Uri.parse(data.getPicUploadPath()))
                            .build();

                    if (canPresentShareDialogWith) {
                        shareDialog.show(linkContent);
                    } else if (profile != null && hasPublishPermission()) {
                        ShareApi.share(linkContent, shareCallback);
                    }


private FacebookCallback<Sharer.Result> shareCallback = new FacebookCallback<Sharer.Result>() {
    @Override
    public void onCancel() {
        Log.d("HelloFacebook", "Canceled");
    }

    @Override
    public void onError(FacebookException error) {
        Log.d("HelloFacebook", String.format("Error: %s", error.toString()));
        String title = getString(R.string.error);
        String alertMessage = error.getMessage();
        showResult(title, alertMessage);
    }

    @Override
    public void onSuccess(Sharer.Result result) {
        Log.d("HelloFacebook", "Success!");
        if (result.getPostId() != null) {
            String title = getString(R.string.success);
            String id = result.getPostId();
            String alertMessage = getString(
                    R.string.successfully_posted_post, id);
            showResult(title, alertMessage);
        }
    }

    private void showResult(String title, String alertMessage) {
        new AlertDialog.Builder(NewsfeedMain.this).setTitle(title)
                .setMessage(alertMessage)
                .setPositiveButton(R.string.ok, null).show();
    }
};

The method here is sharing a link with the respective web url to load , when clicked from FB feed and the image shared is via a link by the native facebook android app or fallback to sdk share dialog is no facebook app is persent.

Upvotes: 0

userDroid
userDroid

Reputation: 198

I think by using facebook sdk 4.0, you can share contents via share api.

eg:

public void share()
{
ShareLinkContent content=new ShareLinkContent.Builder()
.setContentTitle("Your Title")
.setContentUrl(Uri.parse("website link"))
.setImageUrl(Uri.parse("Image url"))
.build();

ShareApi.share(content, new FacebookCallback<Sharer.Result>(){
@Override
public void onSuccess(Result result){

}
@Override
public void onCancel(){

}
@Override
public void onError(FacebookException error){

}
});
}

Upvotes: 0

Miu Octavian
Miu Octavian

Reputation: 95

let s post some code then:

    ShareOpenGraphObject object = new ShareOpenGraphObject.Builder()
            .putString("og:type", "books.book")
            .putString("og:title", "A Game of Thrones")
            .putString("og:description", "In the frozen wastes to the north of Winterfell, sinister and supernatural forces are mustering.")
            .putString("books:isbn", "0-553-57340-3")
            .build();

    ShareOpenGraphAction action = new ShareOpenGraphAction.Builder()
            .setActionType("books.reads")
            .putObject("book", object)
            .putPhoto("image", photo)
            .build();

    ShareOpenGraphContent content = new ShareOpenGraphContent.Builder()
            .setPreviewPropertyName("book")
            .setAction(action)
            .build();

    shareDialog.show(this, content);

idea is that i don t want to use a book, i just want so share a image a link and a message...how the f i do that? facebook sdk sucks

Upvotes: 2

Richa
Richa

Reputation: 3193

Its Simple, You can find many examples as well as in Facebook SDK you can find sample for the same...

Bundle postParams = new Bundle();           
postParams.putString("link", url);
postParams.putString("picture", imgUrl);

    Request request = new Request(session, "me/feed", postParams, 
                    HttpMethod.POST, callback);

            RequestAsyncTask task = new RequestAsyncTask(request);
            task.execute();

Upvotes: 0

Related Questions