<p>This post is about how to enable users of your Android app to share data (links, photos, and videos) from your app to Facebook.</p> 
<p><b>Get Github code from <a href="https://github.com/arunk7839/ShareDataToFacebook"><span style="color: #0000ff;">HERE</span></a>.</b></p> 
<p><amp-youtube layout="responsive" width="1200" height="675" data-videoid="2HI2zL5jEBc" title="Share data to Facebook from Android App Example"><a placeholder href="https://youtu.be/2HI2zL5jEBc"><img src="https://i.ytimg.com/vi/2HI2zL5jEBc/hqdefault.jpg" layout="fill" object-fit="cover" alt="Share data to Facebook from Android App Example"></a></amp-youtube></p> 
<p><strong style="color: #000080; font-size: 20.8px;">The Sharing SDK</strong></p> 
<p>The <strong><span style="color: #008000;">Sharing SDK</span></strong> for Android is used to share data from your app to Facebook. It is a component of the <strong><span style="color: #0000ff;"><a style="color: #0000ff;" href="https://developers.facebook.com/docs/android">Facebook SDK for Android</a></span></strong>.</p> 
<p>When someone shares from your app, the content that they share appears on their Timeline. Content that your users share to their Timeline can also appear in the News Feeds of their friends.</p> 
<p>Follow the steps below to add Facebook Sharing SDK to your app:</p> 
<p>1 . Go to <span style="color: #0000ff;"><a style="color: #0000ff;" href="https://developers.facebook.com/"><strong>facebook developer site</strong></a></span> and log in using your Facebook account.</p> 
<p>2 . After login, click on<strong> <span style="color: #008000;">My Apps</span></strong> option shown at the top right.</p> 
<p>3 . Now, select an app or create a new one.</p> 
<p><img class="alignnone size-full wp-image-3029" src="https://c1ctech.com/wp-content/uploads/2022/02/a.png" alt="" width="658" height="112" /></p> 
<h4><strong><span style="color: #000080;">Import the Facebook SDK</span></strong></h4> 
<p>4 . Open <span style="color: #000080;"><strong>Project-level</strong></span> build.gradle file and add the <strong>mavenCentral()</strong> repository to download the SDK from the Maven Central Repository:</p> 
<pre>buildscript {<br /> repositories {<br /> google()<br /><span style="color: #008000;"><strong> mavenCentral()</strong></span><br /> }</pre> 
<p>5 . Open <strong><span style="color: #000080;">App-level</span></strong> <span style="color: #000000;">build.gradle</span> file and add the latest version of the SDK dependency under the <strong>dependencies</strong> section and then click on <strong><span style="color: #0000ff;">Sync Now</span></strong>.</p> 
<pre>dependencies {<br /> implementation 'com.facebook.android:facebook-share:latest.release'<br />}</pre> 
<h4><strong><span style="color: #000080;">Edit Your Resources and Manifest</span></strong></h4> 
<p>6 . Open your <span style="color: #008000;"><strong>/app/res/values/strings.xml</strong></span> file and add the below string elements:</p> 
<pre><;resources>;<br /> <span style="color: #008000;"><strong><;!--</strong></span><br /><span style="color: #008000;"><strong> On the Dashboard, navigate to Settings >; Basic >; App ID.</strong></span><br /><span style="color: #008000;"><strong> -->;</strong></span><br /> <;string name="facebook_app_id">;"ENTER YOUR APP ID"<;/string>;<br /> <strong><span style="color: #008000;"> <;!--</span></strong><br /><strong><span style="color: #008000;"> On the Dashboard, navigate to Settings >; Advanced >; Security >; Client token.</span></strong><br /><strong><span style="color: #008000;"> -->;</span></strong><br /> <;string name="facebook_client_token">;"ENTER YOUR CLIENT TOKEN"<;/string>;<br /><;/resources>;</pre> 
<p>7 . Open<span style="color: #008000;"><strong> /app/manifest/AndroidManifest.xml</strong></span> file:</p> 
<ul> 
<li>Add meta-data elements to the <strong>application</strong> element for your <strong><span style="color: #008000;">app ID</span></strong> and <span style="color: #008000;"><strong>client access token</strong></span>:</li> 
</ul> 
<pre><;application android:label="@string/app_name" ...>;<br /> ...<br /><;meta-data<br />android:name="com.facebook.sdk.ApplicationId"<br />android:value="@string/facebook_app_id" />;<br /><br /><;meta-data<br />android:name="com.facebook.sdk.ClientToken"<br />android:value="@string/facebook_client_token" />;<br /> ...<br /> <;/application>;</pre> 
<ul> 
<li>Add an activity for <strong><span style="color: #008000;">Facebook</span></strong> inside your <strong>application</strong> element:</li> 
</ul> 
<pre><;activity<br /> android:name="com.facebook.FacebookActivity"<br /> android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"<br /> android:label="@string/app_name" />;</pre> 
<ul> 
<li>Add a uses-permission element for the internet to the manifest above the <strong>application</strong> element:</li> 
</ul> 
<pre><;uses-permission android:name="android.permission.INTERNET" />;</pre> 
<ul> 
<li>Add a <strong><span style="color: #008000;">ContentProvider</span></strong> to your AndroidManifest.xml file and set {APP_ID} to your<strong><span style="color: #008000;"> app ID</span></strong>:</li> 
</ul> 
<pre><;provider android:authorities="com.facebook.app.FacebookContentProvider{APP_ID}"<br />android:name="com.facebook.FacebookContentProvider"<br />android:exported="true"/>;</pre> 
<ul> 
<li>Add the following <strong><span style="color: #008000;">queries</span></strong> block to your <strong>AndroidManifest.xml</strong> file outside the <strong>application</strong> element to make the Facebook App visible to your App:</li> 
</ul> 
<pre><;queries>;<br /> <;provider android:authorities="com.facebook.katana.provider.PlatformProvider" />;<br /><;/queries>;</pre> 
<h4><span style="color: #000080;"><strong>Creating the Layout File</strong></span></h4> 
<p>The below layout file consist of three buttons (to share a link, photo, and video to Facebook).</p> 
<p><strong><span style="color: #0000ff;">activity_main.xml</span></strong></p> 
<pre><;?xml version="1.0" encoding="utf-8"?>;<br /><;androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"<br /> xmlns:app="http://schemas.android.com/apk/res-auto"<br /> xmlns:tools="http://schemas.android.com/tools"<br /> android:layout_width="match_parent"<br /> android:layout_height="match_parent"<br /> tools:context=".MainActivity">;<br /> <br /> <;Button<br /> android:id="@+id/btn_share_link"<br /> android:layout_width="wrap_content"<br /> android:layout_height="wrap_content"<br /> android:text="Share Link"<br /> app:layout_constraintBottom_toBottomOf="parent"<br /> app:layout_constraintEnd_toEndOf="parent"<br /> app:layout_constraintHorizontal_bias="0.5"<br /> app:layout_constraintStart_toStartOf="parent"<br /> app:layout_constraintTop_toTopOf="parent" />;<br /><br /> <;Button<br /> android:id="@+id/btn_share_photo"<br /> android:layout_width="wrap_content"<br /> android:layout_height="wrap_content"<br /> android:layout_marginTop="20dp"<br /> android:text="Share Photo"<br /> app:layout_constraintEnd_toEndOf="parent"<br /> app:layout_constraintHorizontal_bias="0.5"<br /> app:layout_constraintStart_toStartOf="parent"<br /> app:layout_constraintTop_toBottomOf="@+id/btn_share_link" />;<br /><br /> <;Button<br /> android:id="@+id/btn_share_video"<br /> android:layout_width="wrap_content"<br /> android:layout_height="wrap_content"<br /> android:layout_marginTop="20dp"<br /> android:text="Share Video"<br /> app:layout_constraintEnd_toEndOf="parent"<br /> app:layout_constraintHorizontal_bias="0.5"<br /> app:layout_constraintStart_toStartOf="parent"<br /> app:layout_constraintTop_toBottomOf="@+id/btn_share_photo" />;<br /> <br /><;/androidx.constraintlayout.widget.ConstraintLayout>;</pre> 
<p>The UI of the above layout file looks like this:</p> 
<p><img class="alignnone wp-image-3060" src="https://c1ctech.com/wp-content/uploads/2022/02/Screenshot_20220208-210318-485x1024.png" alt="" width="296" height="625" /></p> 
<h4> </h4> 
<h4><span style="color: #000080;"><strong>Creating the Application class</strong></span></h4> 
<p>The <strong><span style="color: #008000;">MyApplication</span></strong> class contains a <strong><span style="color: #0000ff;">printHashKey()</span></strong> method which will create a key hash for your app.</p> 
<p><strong><span style="color: #0000ff;">MyApplication.kt</span></strong></p> 
<pre>package com.c1ctech.sharedatatofacebook<br /><br />import android.app.Application<br />import android.content.pm.PackageManager<br />import android.util.Base64<br />import android.util.Log<br />import java.security.MessageDigest<br />import java.security.NoSuchAlgorithmException<br /><br />class MyApplication: Application() {<br /> override fun onCreate() {<br /> super.onCreate()<br /> printHashKey()<br /> }<br /><br /><strong><span style="color: #008000;"> //Method that prints hash key.</span></strong><br /> fun printHashKey() {<br /> try {<br /> val info = packageManager.getPackageInfo(<br /> "com.c1ctech.sharedatatofacebook",<br /> PackageManager.GET_SIGNATURES<br /> )<br /> for (signature in info.signatures) {<br /> val md: MessageDigest = MessageDigest.getInstance("SHA")<br /> md.update(signature.toByteArray())<br /> Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT))<br /> }<br /> } catch (e: PackageManager.NameNotFoundException) {<br /> } catch (e: NoSuchAlgorithmException) {<br /> }<br /> }<br />}</pre> 
<p>Open the AndroidManifest.xml file and add the application name property as shown below:</p>
<!-- WP QUADS Content Ad Plugin v. 2.0.98.1 -->
<div class="quads-location quads-ad2" id="quads-ad2" style="float:none;margin:0px;">

</div>
 
<pre><;application<br /> android:name=".MyApplication"<br />....<br /><;/application>;</pre> 
<h4><strong><span style="color: #000080;">Adding package name, activity class name, and key hash</span></strong></h4> 
<ul> 
<li>Open your app from <strong><span style="color: #0000ff;"><a style="color: #0000ff;" href="https://developers.facebook.com/apps/">here</a></span></strong>, then go to<span style="color: #008000;"><strong> Settings >; Basic.</strong></span></li> 
<li><span style="color: #008000;"><span style="color: #333333;">Now s</span></span><span style="color: #333333;">croll</span> to the bottom and click on<span style="color: #008000;"><strong> +Add Platform</strong>.</span></li> 
<li>Select the platform to <span style="color: #008000;"><strong>Android</strong></span> and then click on <span style="color: #008000;"><strong>next</strong></span>.</li> 
<li>Add the above created key hash, application package name, and class name. Finally, click on <strong><span style="color: #008000;">save changes</span></strong>:</li> 
</ul> 
<p><img class="alignnone wp-image-3051" src="https://c1ctech.com/wp-content/uploads/2022/02/add_key_hash-1024x673.png" alt="" width="774" height="509" /></p> 
<h4><span style="color: #000080;"><strong>Creating the MainActivity File</strong></span></h4> 
<p>The <span style="color: #008000;"><strong>MainActivity.kt</strong> </span>file contains the below code:</p> 
<p><span style="color: #0000ff;"><strong>MainActivity.kt</strong></span></p> 
<pre>package com.c1ctech.sharedatatofacebook<br /><br />import android.graphics.Bitmap<br />import android.graphics.BitmapFactory<br />import android.net.Uri<br />import androidx.appcompat.app.AppCompatActivity<br />import android.os.Bundle<br />import com.c1ctech.sharedatatofacebook.databinding.ActivityMainBinding<br />import com.facebook.CallbackManager<br />import com.facebook.FacebookSdk<br />import com.facebook.share.widget.ShareDialog<br />import android.content.Intent<br />import com.facebook.share.model.*<br /><br />class MainActivity : AppCompatActivity() {<br /> private val REQUEST_VIDEO_CODE: Int = 1000<br /> lateinit var activityMainBinding: ActivityMainBinding<br /> lateinit var shareDialog: ShareDialog<br /> lateinit var callbackManager: CallbackManager<br /><br /> override fun onCreate(savedInstanceState: Bundle?) {<br /> super.onCreate(savedInstanceState)<br /><br /> activityMainBinding = ActivityMainBinding.inflate(layoutInflater)<br /><br /><strong><span style="color: #008000;"> // initializes the Facebook SDK</span></strong><br /> FacebookSdk.sdkInitialize(applicationContext)<br /><br /> setContentView(activityMainBinding.root)<br /><br /> <strong><span style="color: #008000;">// create an instance of callbackManager</span></strong><br /><strong><span style="color: #008000;"> // It manages the callbacks into the FacebookSdk from an Activity's onActivityResult()'s method.</span></strong><br /> callbackManager = CallbackManager.Factory.create()<br /><br /><strong><span style="color: #008000;"> //create a ShareDialog.</span></strong><br /> shareDialog = ShareDialog(this)<br /><br /><strong><span style="color: #008000;"> //share a link from this app to Facebook</span></strong><br /> activityMainBinding.btnShareLink.setOnClickListener {<br /><br /> val linkContent = ShareLinkContent.Builder()<br /> .setContentUrl(Uri.parse("https://www.youtube.com/"))<br /> .setQuote("Useful link")<br /> .build()<br /><br /><span style="color: #008000;"><strong> //returns True if the ShareLinkContent type can be shown via the dialog</strong></span><br /> if (ShareDialog.canShow(ShareLinkContent::class.java)) {<br /> shareDialog.show(linkContent)<br /> }<br /> }<br /><br /><strong><span style="color: #008000;"> //share a photo from this app to Facebook</span></strong><br /> activityMainBinding.btnSharePhoto.setOnClickListener {<br /><br /> val image: Bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.tiger)<br /> val photo = SharePhoto.Builder()<br /> .setBitmap(image)<br /> .build()<br /><br /><strong><span style="color: #008000;"> //returns True if the SharePhotoContent type can be shown via the dialog</span></strong><br /> if (ShareDialog.canShow(SharePhotoContent::class.java)) {<br /><br /> val photoContent = SharePhotoContent.Builder()<br /> .addPhoto(photo)<br /> .build()<br /> shareDialog.show(photoContent)<br /> }<br /> }<br /><br /><strong><span style="color: #008000;"> //share a video from this app to Facebook</span></strong><br /> activityMainBinding.btnShareVideo.setOnClickListener {<br /><br /><strong><span style="color: #008000;"> // uploading video from phone</span></strong><br /> val sharingIntent = Intent()<br /> sharingIntent.setType("video/*")<br /> sharingIntent.setAction(Intent.ACTION_GET_CONTENT)<br /> startActivityForResult(<br /> Intent.createChooser(sharingIntent, "Select a video"),<br /> REQUEST_VIDEO_CODE<br /> )<br /> }<br /> }<br /><br /> override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {<br /><br /><span style="color: #008000;"><strong> // call the SDK's callbackManager in your onActivityResult to handle the response</strong></span><br /> callbackManager.onActivityResult(requestCode, resultCode, data)<br /> super.onActivityResult(requestCode, resultCode, data)<br /><br /> if (resultCode == RESULT_OK) {<br /> if (requestCode == REQUEST_VIDEO_CODE) {<br /> val videoUrl = data?.data<br /><br /> val video = ShareVideo.Builder()<br /> .setLocalUrl(videoUrl)<br /> .build()<br /><br /> val videoContent = ShareVideoContent.Builder()<br /> .setVideo(video)<br /> .setContentTitle("This is useful video")<br /> .setContentDescription("Video made by me")<br /> .build()<br /><br /><strong><span style="color: #008000;"> //returns True if the ShareVideoContent type can be shown via the dialog</span></strong><br /> if (ShareDialog.canShow(ShareVideoContent::class.java)) {<br /> shareDialog.show(videoContent)<br /> }<br /> }<br /> }<br /> }<br />}</pre> 
<p>In the above code,</p> 
<ul> 
<li><strong><span style="color: #008000;">sdkInitialize()</span></strong> method initializes the Facebook SDK.</li> 
<li>The instance of <strong><span style="color: #008000;">callbackManager</span></strong> manages the callbacks into the FacebookSdk from the Activity’s <strong><span style="color: #008000;">onActivityResult()</span> </strong>method.</li> 
<li>To share <strong><span style="color: #008000;">links</span></strong>, we need to create a <strong><span style="color: #008000;">ShareLinkContent</span></strong> object which describes link content to be shared.</li> 
<li>To share <strong><span style="color: #008000;">photos</span></strong>, we need to create a <strong><span style="color: #008000;">SharePhotoContent</span></strong> object which describes photo content to be shared.</li> 
<li>To share <strong><span style="color: #008000;">videos</span></strong>, we need to create a <strong><span style="color: #008000;">ShareVideoContent</span></strong> object which Provides the interface for video content to be shared.</li> 
<li>The <strong><span style="color: #008000;">Share dialog</span></strong> switches to the native Facebook for Android app then returns control to your app after a post is published.</li> 
<li>The share dialog <span style="color: #008000;"><strong>canShow()</strong></span> method returns <strong><span style="color: #008000;">true</span></strong> if the specified content type can be shown via the dialog.</li> 
</ul> 
<p>Given below is the screenshot of the app when you share a link to Facebook.</p> 
<p><img class="alignnone wp-image-3055" src="https://c1ctech.com/wp-content/uploads/2022/02/Screenshot_20220208-152539-485x1024.png" alt="" width="312" height="658" /></p> 
<p>Similarly, when you share a photo and a video to Facebook, it will look like this:</p> 
<p><img class="alignnone wp-image-3056" src="https://c1ctech.com/wp-content/uploads/2022/02/Screenshot_20220208-152618-485x1024.png" alt="" width="322" height="680" /> <img class="alignnone wp-image-3057" src="https://c1ctech.com/wp-content/uploads/2022/02/Screenshot_20220208-152746-485x1024.png" alt="" width="318" height="670" />

