Share Images or Content To Facebook From Android Application Tutorial using Facebook SDK

Total Views : 410
Zoom In Zoom Out Read Later Print

This tutorial helps you to share content or images from your Android Application to Facebook. In the previous tutorial Android Facebook Integration and Login Tutorial, I explained how to integrate Android Application to Facebook and how to login through Facebook button.

You can read previous tutorial from this URL.

ContentProvider

You also need to set up a ContentProvider in your AndroidManifest.xml where {APP_ID} is your app ID. Without using ContentProvider, we can not share any content on the Timeline.

ContentProvider

<provider android:authorities="com.facebook.app.FacebookContentProvider{@string/facebook_app_id}"
android:name="com.facebook.FacebookContentProvider"
android:exported="true"/>

acitivity_user_profile.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
tools:context=".UserProfile">

<ImageView
android:layout_width="120dp"
android:layout_height="120dp"
android:id="@+id/profilePic"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/UserName"
android:layout_toRightOf="@+id/profilePic"
android:padding="5dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/email"
android:layout_below="@+id/UserName"
android:layout_toRightOf="@+id/profilePic"
android:padding="5dp"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Share Content on Facebook"
android:background="#0055FF"
android:textColor="#fff"
android:textStyle="bold"
android:id="@+id/btn_share"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Share Image on Facebook"
android:background="#0055FF"
android:textColor="#fff"
android:textStyle="bold"
android:layout_below="@+id/btn_share"
android:id="@+id/btn_share_img"
android:layout_marginTop="5dp"/>
</RelativeLayout>

ShareDialog

We need to initialize a Share dialog to share the content on the Facebook from Android Device. The Share dialog open a popup with content, create a ShareDialog instance in your onCreate method then show the ShareDialog. Use the following code in UserProfile.java

ShareDialog

ShareDialog shareDialog;
CallbackManager callbackManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_profile);
init();
}
void init(){
callbackManager = CallbackManager.Factory.create();
shareDialog = new ShareDialog(this);
shareDialog.registerCallback(callbackManager, callback);
}

Share Content on Facebook

Contant Share

if (ShareDialog.canShow(ShareLinkContent.class)) {
ShareLinkContent linkContent = new ShareLinkContent.Builder()
.setContentUrl(Uri.parse("http://www.prodevsblog.com/view/14/login-with-facebook-android-studio-using-facebook-sdk/"))
.build();
shareDialog.show(linkContent); // Show ShareDialog
}

Share Image on Facebook

Image Share

Bitmap image = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
SharePhoto photo = new SharePhoto.Builder()
.setBitmap(image)
.setCaption("#ProDevsBlog.com")
.build();
SharePhotoContent content = new SharePhotoContent.Builder()
.addPhoto(photo)
.build();
shareDialog.show(content);

Handle the response

Response

protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}

Full Activity

UserProfile.java

package com.prodevsblog.facebookshare;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.share.Sharer;
import com.facebook.share.model.ShareLinkContent;
import com.facebook.share.model.SharePhoto;
import com.facebook.share.model.SharePhotoContent;
import com.facebook.share.widget.ShareDialog;
import com.squareup.picasso.Picasso;

import org.json.JSONObject;

public class UserProfile extends AppCompatActivity {

JSONObject response, profile_pic_data, profile_pic_url;
ShareDialog shareDialog;
CallbackManager callbackManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_profile);
init();
}
void init(){
callbackManager = CallbackManager.Factory.create();
Intent intent = getIntent();
String jsondata = intent.getStringExtra("userProfile");
Log.w("Jsondata", jsondata);
TextView user_name = findViewById(R.id.UserName);
ImageView user_picture = findViewById(R.id.profilePic);
TextView user_email = findViewById(R.id.email);
try {
response = new JSONObject(jsondata);
user_email.setText(response.get("email").toString());
user_name.setText(response.get("name").toString());
profile_pic_data = new JSONObject(response.get("picture").toString());
profile_pic_url = new JSONObject(profile_pic_data.getString("data"));
Picasso.with(this).load(profile_pic_url.getString("url"))
.into(user_picture);

} catch(Exception e){
e.printStackTrace();
}

shareDialog = new ShareDialog(this);
shareDialog.registerCallback(callbackManager, callback);
findViewById(R.id.btn_share).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
contentShare();
}
});
findViewById(R.id.btn_share_img).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
imageShare();
}
});
}
void contentShare(){
if (ShareDialog.canShow(ShareLinkContent.class)) {
ShareLinkContent linkContent = new ShareLinkContent.Builder()
.setContentUrl(Uri.parse("http://www.prodevsblog.com/view/14/login-with-facebook-android-studio-using-facebook-sdk/"))
.build();
shareDialog.show(linkContent); // Show ShareDialog
}
}
void imageShare(){
Bitmap image = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
SharePhoto photo = new SharePhoto.Builder()
.setBitmap(image)
.setCaption("#ProDevsBlog.com")
.build();
SharePhotoContent content = new SharePhotoContent.Builder()
.addPhoto(photo)
.build();
shareDialog.show(content);
}
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
private FacebookCallback<Sharer.Result> callback = new FacebookCallback<Sharer.Result>() {
@Override
public void onSuccess(Sharer.Result result) {
Log.v("UserProfile", "Successfully posted");
// Write some code to do some operations when you shared content successfully.
}

@Override
public void onCancel() {
Log.v("UserProfile", "Sharing cancelled");
// Write some code to do some operations when you cancel sharing content.
}

@Override
public void onError(FacebookException error) {
Log.v("UserProfile", error.getMessage());
// Write some code to do some operations when some error occurs while sharing content.
}
};
}

See More

Latest Photos