Site icon C1CTech

Share data to Facebook from Android App Example

&NewLine;<p>This post is about how to enable users of your Android app to share data &lpar;links&comma; photos&comma; and videos&rpar; from your app to Facebook&period;<&sol;p> &NewLine;<p><b>Get Github code from <a href&equals;"https&colon;&sol;&sol;github&period;com&sol;arunk7839&sol;ShareDataToFacebook"><span style&equals;"color&colon; &num;0000ff&semi;">HERE<&sol;span><&sol;a>&period;<&sol;b><&sol;p> &NewLine;<p><amp-youtube layout&equals;"responsive" width&equals;"1200" height&equals;"675" data-videoid&equals;"2HI2zL5jEBc" title&equals;"Share data to Facebook from Android App Example"><a placeholder href&equals;"https&colon;&sol;&sol;youtu&period;be&sol;2HI2zL5jEBc"><img src&equals;"https&colon;&sol;&sol;i&period;ytimg&period;com&sol;vi&sol;2HI2zL5jEBc&sol;hqdefault&period;jpg" layout&equals;"fill" object-fit&equals;"cover" alt&equals;"Share data to Facebook from Android App Example"><&sol;a><&sol;amp-youtube><&sol;p> &NewLine;<p><strong style&equals;"color&colon; &num;000080&semi; font-size&colon; 20&period;8px&semi;">The Sharing SDK<&sol;strong><&sol;p> &NewLine;<p>The <strong><span style&equals;"color&colon; &num;008000&semi;">Sharing SDK<&sol;span><&sol;strong> for Android is used to share data from your app to Facebook&period; It is a component of the <strong><span style&equals;"color&colon; &num;0000ff&semi;"><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;developers&period;facebook&period;com&sol;docs&sol;android">Facebook SDK for Android<&sol;a><&sol;span><&sol;strong>&period;<&sol;p> &NewLine;<p>When someone shares from your app&comma; the content that they share appears on their Timeline&period; Content that your users share to their Timeline can also appear in the News Feeds of their friends&period;<&sol;p> &NewLine;<p>Follow the steps below to add Facebook Sharing SDK to your app&colon;<&sol;p> &NewLine;<p>1 &period; Go to <span style&equals;"color&colon; &num;0000ff&semi;"><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;developers&period;facebook&period;com&sol;"><strong>facebook developer site<&sol;strong><&sol;a><&sol;span> and log in using your Facebook account&period;<&sol;p> &NewLine;<p>2 &period; After login&comma; click on<strong> <span style&equals;"color&colon; &num;008000&semi;">My Apps<&sol;span><&sol;strong> option shown at the top right&period;<&sol;p> &NewLine;<p>3 &period; Now&comma; select an app or create a new one&period;<&sol;p> &NewLine;<p><img class&equals;"alignnone size-full wp-image-3029" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2022&sol;02&sol;a&period;png" alt&equals;"" width&equals;"658" height&equals;"112" &sol;><&sol;p> &NewLine;<h4><strong><span style&equals;"color&colon; &num;000080&semi;">Import the Facebook SDK<&sol;span><&sol;strong><&sol;h4> &NewLine;<p>4 &period; Open <span style&equals;"color&colon; &num;000080&semi;"><strong>Project-level<&sol;strong><&sol;span> build&period;gradle file and add the <strong>mavenCentral&lpar;&rpar;<&sol;strong> repository to download the SDK from the Maven Central Repository&colon;<&sol;p> &NewLine;<pre>buildscript &lbrace;<br &sol;> repositories &lbrace;<br &sol;> google&lpar;&rpar;<br &sol;><span style&equals;"color&colon; &num;008000&semi;"><strong> mavenCentral&lpar;&rpar;<&sol;strong><&sol;span><br &sol;> &rcub;<&sol;pre> &NewLine;<p>5 &period; Open <strong><span style&equals;"color&colon; &num;000080&semi;">App-level<&sol;span><&sol;strong> <span style&equals;"color&colon; &num;000000&semi;">build&period;gradle<&sol;span> file and add the latest version of the SDK dependency under the <strong>dependencies<&sol;strong> section and then click on <strong><span style&equals;"color&colon; &num;0000ff&semi;">Sync Now<&sol;span><&sol;strong>&period;<&sol;p> &NewLine;<pre>dependencies &lbrace;<br &sol;> implementation 'com&period;facebook&period;android&colon;facebook-share&colon;latest&period;release'<br &sol;>&rcub;<&sol;pre> &NewLine;<h4><strong><span style&equals;"color&colon; &num;000080&semi;">Edit Your Resources and Manifest<&sol;span><&sol;strong><&sol;h4> &NewLine;<p>6 &period; Open your <span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;app&sol;res&sol;values&sol;strings&period;xml<&sol;strong><&sol;span> file and add the below string elements&colon;<&sol;p> &NewLine;<pre>&lt&semi;resources&gt&semi;<br &sol;> <span style&equals;"color&colon; &num;008000&semi;"><strong>&lt&semi;&excl;--<&sol;strong><&sol;span><br &sol;><span style&equals;"color&colon; &num;008000&semi;"><strong> On the Dashboard&comma; navigate to Settings &gt&semi; Basic &gt&semi; App ID&period;<&sol;strong><&sol;span><br &sol;><span style&equals;"color&colon; &num;008000&semi;"><strong> --&gt&semi;<&sol;strong><&sol;span><br &sol;> &lt&semi;string name&equals;"facebook&lowbar;app&lowbar;id"&gt&semi;"ENTER YOUR APP ID"&lt&semi;&sol;string&gt&semi;<br &sol;> <strong><span style&equals;"color&colon; &num;008000&semi;"> &lt&semi;&excl;--<&sol;span><&sol;strong><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> On the Dashboard&comma; navigate to Settings &gt&semi; Advanced &gt&semi; Security &gt&semi; Client token&period;<&sol;span><&sol;strong><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> --&gt&semi;<&sol;span><&sol;strong><br &sol;> &lt&semi;string name&equals;"facebook&lowbar;client&lowbar;token"&gt&semi;"ENTER YOUR CLIENT TOKEN"&lt&semi;&sol;string&gt&semi;<br &sol;>&lt&semi;&sol;resources&gt&semi;<&sol;pre> &NewLine;<p>7 &period; Open<span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;app&sol;manifest&sol;AndroidManifest&period;xml<&sol;strong><&sol;span> file&colon;<&sol;p> &NewLine;<ul> &NewLine;<li>Add meta-data elements to the <strong>application<&sol;strong> element for your <strong><span style&equals;"color&colon; &num;008000&semi;">app ID<&sol;span><&sol;strong> and <span style&equals;"color&colon; &num;008000&semi;"><strong>client access token<&sol;strong><&sol;span>&colon;<&sol;li> &NewLine;<&sol;ul> &NewLine;<pre>&lt&semi;application android&colon;label&equals;"&commat;string&sol;app&lowbar;name" &period;&period;&period;&gt&semi;<br &sol;> &period;&period;&period;<br &sol;>&lt&semi;meta-data<br &sol;>android&colon;name&equals;"com&period;facebook&period;sdk&period;ApplicationId"<br &sol;>android&colon;value&equals;"&commat;string&sol;facebook&lowbar;app&lowbar;id" &sol;&gt&semi;<br &sol;><br &sol;>&lt&semi;meta-data<br &sol;>android&colon;name&equals;"com&period;facebook&period;sdk&period;ClientToken"<br &sol;>android&colon;value&equals;"&commat;string&sol;facebook&lowbar;client&lowbar;token" &sol;&gt&semi;<br &sol;> &period;&period;&period;<br &sol;> &lt&semi;&sol;application&gt&semi;<&sol;pre> &NewLine;<ul> &NewLine;<li>Add an activity for <strong><span style&equals;"color&colon; &num;008000&semi;">Facebook<&sol;span><&sol;strong> inside your <strong>application<&sol;strong> element&colon;<&sol;li> &NewLine;<&sol;ul> &NewLine;<pre>&lt&semi;activity<br &sol;> android&colon;name&equals;"com&period;facebook&period;FacebookActivity"<br &sol;> android&colon;configChanges&equals;"keyboard&vert;keyboardHidden&vert;screenLayout&vert;screenSize&vert;orientation"<br &sol;> android&colon;label&equals;"&commat;string&sol;app&lowbar;name" &sol;&gt&semi;<&sol;pre> &NewLine;<ul> &NewLine;<li>Add a uses-permission element for the internet to the manifest above the <strong>application<&sol;strong> element&colon;<&sol;li> &NewLine;<&sol;ul> &NewLine;<pre>&lt&semi;uses-permission android&colon;name&equals;"android&period;permission&period;INTERNET" &sol;&gt&semi;<&sol;pre> &NewLine;<ul> &NewLine;<li>Add a <strong><span style&equals;"color&colon; &num;008000&semi;">ContentProvider<&sol;span><&sol;strong> to your AndroidManifest&period;xml file and set &lbrace;APP&lowbar;ID&rcub; to your<strong><span style&equals;"color&colon; &num;008000&semi;"> app ID<&sol;span><&sol;strong>&colon;<&sol;li> &NewLine;<&sol;ul> &NewLine;<pre>&lt&semi;provider android&colon;authorities&equals;"com&period;facebook&period;app&period;FacebookContentProvider&lbrace;APP&lowbar;ID&rcub;"<br &sol;>android&colon;name&equals;"com&period;facebook&period;FacebookContentProvider"<br &sol;>android&colon;exported&equals;"true"&sol;&gt&semi;<&sol;pre> &NewLine;<ul> &NewLine;<li>Add the following <strong><span style&equals;"color&colon; &num;008000&semi;">queries<&sol;span><&sol;strong> block to your <strong>AndroidManifest&period;xml<&sol;strong> file outside the <strong>application<&sol;strong> element to make the Facebook App visible to your App&colon;<&sol;li> &NewLine;<&sol;ul> &NewLine;<pre>&lt&semi;queries&gt&semi;<br &sol;> &lt&semi;provider android&colon;authorities&equals;"com&period;facebook&period;katana&period;provider&period;PlatformProvider" &sol;&gt&semi;<br &sol;>&lt&semi;&sol;queries&gt&semi;<&sol;pre> &NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>Creating the Layout File<&sol;strong><&sol;span><&sol;h4> &NewLine;<p>The below layout file consist of three buttons &lpar;to share a link&comma; photo&comma; and video to Facebook&rpar;&period;<&sol;p> &NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">activity&lowbar;main&period;xml<&sol;span><&sol;strong><&sol;p> &NewLine;<pre>&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"utf-8"&quest;&gt&semi;<br &sol;>&lt&semi;androidx&period;constraintlayout&period;widget&period;ConstraintLayout xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"<br &sol;> xmlns&colon;app&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res-auto"<br &sol;> xmlns&colon;tools&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;tools"<br &sol;> android&colon;layout&lowbar;width&equals;"match&lowbar;parent"<br &sol;> android&colon;layout&lowbar;height&equals;"match&lowbar;parent"<br &sol;> tools&colon;context&equals;"&period;MainActivity"&gt&semi;<br &sol;> <br &sol;> &lt&semi;Button<br &sol;> android&colon;id&equals;"&commat;&plus;id&sol;btn&lowbar;share&lowbar;link"<br &sol;> android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"<br &sol;> android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"<br &sol;> android&colon;text&equals;"Share Link"<br &sol;> app&colon;layout&lowbar;constraintBottom&lowbar;toBottomOf&equals;"parent"<br &sol;> app&colon;layout&lowbar;constraintEnd&lowbar;toEndOf&equals;"parent"<br &sol;> app&colon;layout&lowbar;constraintHorizontal&lowbar;bias&equals;"0&period;5"<br &sol;> app&colon;layout&lowbar;constraintStart&lowbar;toStartOf&equals;"parent"<br &sol;> app&colon;layout&lowbar;constraintTop&lowbar;toTopOf&equals;"parent" &sol;&gt&semi;<br &sol;><br &sol;> &lt&semi;Button<br &sol;> android&colon;id&equals;"&commat;&plus;id&sol;btn&lowbar;share&lowbar;photo"<br &sol;> android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"<br &sol;> android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"<br &sol;> android&colon;layout&lowbar;marginTop&equals;"20dp"<br &sol;> android&colon;text&equals;"Share Photo"<br &sol;> app&colon;layout&lowbar;constraintEnd&lowbar;toEndOf&equals;"parent"<br &sol;> app&colon;layout&lowbar;constraintHorizontal&lowbar;bias&equals;"0&period;5"<br &sol;> app&colon;layout&lowbar;constraintStart&lowbar;toStartOf&equals;"parent"<br &sol;> app&colon;layout&lowbar;constraintTop&lowbar;toBottomOf&equals;"&commat;&plus;id&sol;btn&lowbar;share&lowbar;link" &sol;&gt&semi;<br &sol;><br &sol;> &lt&semi;Button<br &sol;> android&colon;id&equals;"&commat;&plus;id&sol;btn&lowbar;share&lowbar;video"<br &sol;> android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"<br &sol;> android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"<br &sol;> android&colon;layout&lowbar;marginTop&equals;"20dp"<br &sol;> android&colon;text&equals;"Share Video"<br &sol;> app&colon;layout&lowbar;constraintEnd&lowbar;toEndOf&equals;"parent"<br &sol;> app&colon;layout&lowbar;constraintHorizontal&lowbar;bias&equals;"0&period;5"<br &sol;> app&colon;layout&lowbar;constraintStart&lowbar;toStartOf&equals;"parent"<br &sol;> app&colon;layout&lowbar;constraintTop&lowbar;toBottomOf&equals;"&commat;&plus;id&sol;btn&lowbar;share&lowbar;photo" &sol;&gt&semi;<br &sol;> <br &sol;>&lt&semi;&sol;androidx&period;constraintlayout&period;widget&period;ConstraintLayout&gt&semi;<&sol;pre> &NewLine;<p>The UI of the above layout file looks like this&colon;<&sol;p> &NewLine;<p><img class&equals;"alignnone wp-image-3060" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2022&sol;02&sol;Screenshot&lowbar;20220208-210318-485x1024&period;png" alt&equals;"" width&equals;"296" height&equals;"625" &sol;><&sol;p> &NewLine;<h4> <&sol;h4> &NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>Creating the Application class<&sol;strong><&sol;span><&sol;h4> &NewLine;<p>The <strong><span style&equals;"color&colon; &num;008000&semi;">MyApplication<&sol;span><&sol;strong> class contains a <strong><span style&equals;"color&colon; &num;0000ff&semi;">printHashKey&lpar;&rpar;<&sol;span><&sol;strong> method which will create a key hash for your app&period;<&sol;p> &NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">MyApplication&period;kt<&sol;span><&sol;strong><&sol;p> &NewLine;<pre>package com&period;c1ctech&period;sharedatatofacebook<br &sol;><br &sol;>import android&period;app&period;Application<br &sol;>import android&period;content&period;pm&period;PackageManager<br &sol;>import android&period;util&period;Base64<br &sol;>import android&period;util&period;Log<br &sol;>import java&period;security&period;MessageDigest<br &sol;>import java&period;security&period;NoSuchAlgorithmException<br &sol;><br &sol;>class MyApplication&colon; Application&lpar;&rpar; &lbrace;<br &sol;> override fun onCreate&lpar;&rpar; &lbrace;<br &sol;> super&period;onCreate&lpar;&rpar;<br &sol;> printHashKey&lpar;&rpar;<br &sol;> &rcub;<br &sol;><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;Method that prints hash key&period;<&sol;span><&sol;strong><br &sol;> fun printHashKey&lpar;&rpar; &lbrace;<br &sol;> try &lbrace;<br &sol;> val info &equals; packageManager&period;getPackageInfo&lpar;<br &sol;> "com&period;c1ctech&period;sharedatatofacebook"&comma;<br &sol;> PackageManager&period;GET&lowbar;SIGNATURES<br &sol;> &rpar;<br &sol;> for &lpar;signature in info&period;signatures&rpar; &lbrace;<br &sol;> val md&colon; MessageDigest &equals; MessageDigest&period;getInstance&lpar;"SHA"&rpar;<br &sol;> md&period;update&lpar;signature&period;toByteArray&lpar;&rpar;&rpar;<br &sol;> Log&period;d&lpar;"KeyHash&colon;"&comma; Base64&period;encodeToString&lpar;md&period;digest&lpar;&rpar;&comma; Base64&period;DEFAULT&rpar;&rpar;<br &sol;> &rcub;<br &sol;> &rcub; catch &lpar;e&colon; PackageManager&period;NameNotFoundException&rpar; &lbrace;<br &sol;> &rcub; catch &lpar;e&colon; NoSuchAlgorithmException&rpar; &lbrace;<br &sol;> &rcub;<br &sol;> &rcub;<br &sol;>&rcub;<&sol;pre> &NewLine;<p>Open the AndroidManifest&period;xml file and add the application name property as shown below&colon;<&sol;p>&NewLine;<&excl;-- WP QUADS Content Ad Plugin v&period; 2&period;0&period;98&period;1 -->&NewLine;<div class&equals;"quads-location quads-ad2" id&equals;"quads-ad2" style&equals;"float&colon;none&semi;margin&colon;0px&semi;">&NewLine;&NewLine;<&sol;div>&NewLine; &NewLine;<pre>&lt&semi;application<br &sol;> android&colon;name&equals;"&period;MyApplication"<br &sol;>&period;&period;&period;&period;<br &sol;>&lt&semi;&sol;application&gt&semi;<&sol;pre> &NewLine;<h4><strong><span style&equals;"color&colon; &num;000080&semi;">Adding package name&comma; activity class name&comma; and key hash<&sol;span><&sol;strong><&sol;h4> &NewLine;<ul> &NewLine;<li>Open your app from <strong><span style&equals;"color&colon; &num;0000ff&semi;"><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;developers&period;facebook&period;com&sol;apps&sol;">here<&sol;a><&sol;span><&sol;strong>&comma; then go to<span style&equals;"color&colon; &num;008000&semi;"><strong> Settings &gt&semi; Basic&period;<&sol;strong><&sol;span><&sol;li> &NewLine;<li><span style&equals;"color&colon; &num;008000&semi;"><span style&equals;"color&colon; &num;333333&semi;">Now s<&sol;span><&sol;span><span style&equals;"color&colon; &num;333333&semi;">croll<&sol;span> to the bottom and click on<span style&equals;"color&colon; &num;008000&semi;"><strong> &plus;Add Platform<&sol;strong>&period;<&sol;span><&sol;li> &NewLine;<li>Select the platform to <span style&equals;"color&colon; &num;008000&semi;"><strong>Android<&sol;strong><&sol;span> and then click on <span style&equals;"color&colon; &num;008000&semi;"><strong>next<&sol;strong><&sol;span>&period;<&sol;li> &NewLine;<li>Add the above created key hash&comma; application package name&comma; and class name&period; Finally&comma; click on <strong><span style&equals;"color&colon; &num;008000&semi;">save changes<&sol;span><&sol;strong>&colon;<&sol;li> &NewLine;<&sol;ul> &NewLine;<p><img class&equals;"alignnone wp-image-3051" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2022&sol;02&sol;add&lowbar;key&lowbar;hash-1024x673&period;png" alt&equals;"" width&equals;"774" height&equals;"509" &sol;><&sol;p> &NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>Creating the MainActivity File<&sol;strong><&sol;span><&sol;h4> &NewLine;<p>The <span style&equals;"color&colon; &num;008000&semi;"><strong>MainActivity&period;kt<&sol;strong> <&sol;span>file contains the below code&colon;<&sol;p> &NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>MainActivity&period;kt<&sol;strong><&sol;span><&sol;p> &NewLine;<pre>package com&period;c1ctech&period;sharedatatofacebook<br &sol;><br &sol;>import android&period;graphics&period;Bitmap<br &sol;>import android&period;graphics&period;BitmapFactory<br &sol;>import android&period;net&period;Uri<br &sol;>import androidx&period;appcompat&period;app&period;AppCompatActivity<br &sol;>import android&period;os&period;Bundle<br &sol;>import com&period;c1ctech&period;sharedatatofacebook&period;databinding&period;ActivityMainBinding<br &sol;>import com&period;facebook&period;CallbackManager<br &sol;>import com&period;facebook&period;FacebookSdk<br &sol;>import com&period;facebook&period;share&period;widget&period;ShareDialog<br &sol;>import android&period;content&period;Intent<br &sol;>import com&period;facebook&period;share&period;model&period;&ast;<br &sol;><br &sol;>class MainActivity &colon; AppCompatActivity&lpar;&rpar; &lbrace;<br &sol;> private val REQUEST&lowbar;VIDEO&lowbar;CODE&colon; Int &equals; 1000<br &sol;> lateinit var activityMainBinding&colon; ActivityMainBinding<br &sol;> lateinit var shareDialog&colon; ShareDialog<br &sol;> lateinit var callbackManager&colon; CallbackManager<br &sol;><br &sol;> override fun onCreate&lpar;savedInstanceState&colon; Bundle&quest;&rpar; &lbrace;<br &sol;> super&period;onCreate&lpar;savedInstanceState&rpar;<br &sol;><br &sol;> activityMainBinding &equals; ActivityMainBinding&period;inflate&lpar;layoutInflater&rpar;<br &sol;><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol; initializes the Facebook SDK<&sol;span><&sol;strong><br &sol;> FacebookSdk&period;sdkInitialize&lpar;applicationContext&rpar;<br &sol;><br &sol;> setContentView&lpar;activityMainBinding&period;root&rpar;<br &sol;><br &sol;> <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; create an instance of callbackManager<&sol;span><&sol;strong><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol; It manages the callbacks into the FacebookSdk from an Activity's onActivityResult&lpar;&rpar;'s method&period;<&sol;span><&sol;strong><br &sol;> callbackManager &equals; CallbackManager&period;Factory&period;create&lpar;&rpar;<br &sol;><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;create a ShareDialog&period;<&sol;span><&sol;strong><br &sol;> shareDialog &equals; ShareDialog&lpar;this&rpar;<br &sol;><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;share a link from this app to Facebook<&sol;span><&sol;strong><br &sol;> activityMainBinding&period;btnShareLink&period;setOnClickListener &lbrace;<br &sol;><br &sol;> val linkContent &equals; ShareLinkContent&period;Builder&lpar;&rpar;<br &sol;> &period;setContentUrl&lpar;Uri&period;parse&lpar;"https&colon;&sol;&sol;www&period;youtube&period;com&sol;"&rpar;&rpar;<br &sol;> &period;setQuote&lpar;"Useful link"&rpar;<br &sol;> &period;build&lpar;&rpar;<br &sol;><br &sol;><span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;returns True if the ShareLinkContent type can be shown via the dialog<&sol;strong><&sol;span><br &sol;> if &lpar;ShareDialog&period;canShow&lpar;ShareLinkContent&colon;&colon;class&period;java&rpar;&rpar; &lbrace;<br &sol;> shareDialog&period;show&lpar;linkContent&rpar;<br &sol;> &rcub;<br &sol;> &rcub;<br &sol;><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;share a photo from this app to Facebook<&sol;span><&sol;strong><br &sol;> activityMainBinding&period;btnSharePhoto&period;setOnClickListener &lbrace;<br &sol;><br &sol;> val image&colon; Bitmap &equals; BitmapFactory&period;decodeResource&lpar;getResources&lpar;&rpar;&comma; R&period;drawable&period;tiger&rpar;<br &sol;> val photo &equals; SharePhoto&period;Builder&lpar;&rpar;<br &sol;> &period;setBitmap&lpar;image&rpar;<br &sol;> &period;build&lpar;&rpar;<br &sol;><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;returns True if the SharePhotoContent type can be shown via the dialog<&sol;span><&sol;strong><br &sol;> if &lpar;ShareDialog&period;canShow&lpar;SharePhotoContent&colon;&colon;class&period;java&rpar;&rpar; &lbrace;<br &sol;><br &sol;> val photoContent &equals; SharePhotoContent&period;Builder&lpar;&rpar;<br &sol;> &period;addPhoto&lpar;photo&rpar;<br &sol;> &period;build&lpar;&rpar;<br &sol;> shareDialog&period;show&lpar;photoContent&rpar;<br &sol;> &rcub;<br &sol;> &rcub;<br &sol;><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;share a video from this app to Facebook<&sol;span><&sol;strong><br &sol;> activityMainBinding&period;btnShareVideo&period;setOnClickListener &lbrace;<br &sol;><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol; uploading video from phone<&sol;span><&sol;strong><br &sol;> val sharingIntent &equals; Intent&lpar;&rpar;<br &sol;> sharingIntent&period;setType&lpar;"video&sol;&ast;"&rpar;<br &sol;> sharingIntent&period;setAction&lpar;Intent&period;ACTION&lowbar;GET&lowbar;CONTENT&rpar;<br &sol;> startActivityForResult&lpar;<br &sol;> Intent&period;createChooser&lpar;sharingIntent&comma; "Select a video"&rpar;&comma;<br &sol;> REQUEST&lowbar;VIDEO&lowbar;CODE<br &sol;> &rpar;<br &sol;> &rcub;<br &sol;> &rcub;<br &sol;><br &sol;> override fun onActivityResult&lpar;requestCode&colon; Int&comma; resultCode&colon; Int&comma; data&colon; Intent&quest;&rpar; &lbrace;<br &sol;><br &sol;><span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol; call the SDK's callbackManager in your onActivityResult to handle the response<&sol;strong><&sol;span><br &sol;> callbackManager&period;onActivityResult&lpar;requestCode&comma; resultCode&comma; data&rpar;<br &sol;> super&period;onActivityResult&lpar;requestCode&comma; resultCode&comma; data&rpar;<br &sol;><br &sol;> if &lpar;resultCode &equals;&equals; RESULT&lowbar;OK&rpar; &lbrace;<br &sol;> if &lpar;requestCode &equals;&equals; REQUEST&lowbar;VIDEO&lowbar;CODE&rpar; &lbrace;<br &sol;> val videoUrl &equals; data&quest;&period;data<br &sol;><br &sol;> val video &equals; ShareVideo&period;Builder&lpar;&rpar;<br &sol;> &period;setLocalUrl&lpar;videoUrl&rpar;<br &sol;> &period;build&lpar;&rpar;<br &sol;><br &sol;> val videoContent &equals; ShareVideoContent&period;Builder&lpar;&rpar;<br &sol;> &period;setVideo&lpar;video&rpar;<br &sol;> &period;setContentTitle&lpar;"This is useful video"&rpar;<br &sol;> &period;setContentDescription&lpar;"Video made by me"&rpar;<br &sol;> &period;build&lpar;&rpar;<br &sol;><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;returns True if the ShareVideoContent type can be shown via the dialog<&sol;span><&sol;strong><br &sol;> if &lpar;ShareDialog&period;canShow&lpar;ShareVideoContent&colon;&colon;class&period;java&rpar;&rpar; &lbrace;<br &sol;> shareDialog&period;show&lpar;videoContent&rpar;<br &sol;> &rcub;<br &sol;> &rcub;<br &sol;> &rcub;<br &sol;> &rcub;<br &sol;>&rcub;<&sol;pre> &NewLine;<p>In the above code&comma;<&sol;p> &NewLine;<ul> &NewLine;<li><strong><span style&equals;"color&colon; &num;008000&semi;">sdkInitialize&lpar;&rpar;<&sol;span><&sol;strong> method initializes the Facebook SDK&period;<&sol;li> &NewLine;<li>The instance of <strong><span style&equals;"color&colon; &num;008000&semi;">callbackManager<&sol;span><&sol;strong> manages the callbacks into the FacebookSdk from the Activity’s <strong><span style&equals;"color&colon; &num;008000&semi;">onActivityResult&lpar;&rpar;<&sol;span> <&sol;strong>method&period;<&sol;li> &NewLine;<li>To share <strong><span style&equals;"color&colon; &num;008000&semi;">links<&sol;span><&sol;strong>&comma; we need to create a <strong><span style&equals;"color&colon; &num;008000&semi;">ShareLinkContent<&sol;span><&sol;strong> object which describes link content to be shared&period;<&sol;li> &NewLine;<li>To share <strong><span style&equals;"color&colon; &num;008000&semi;">photos<&sol;span><&sol;strong>&comma; we need to create a <strong><span style&equals;"color&colon; &num;008000&semi;">SharePhotoContent<&sol;span><&sol;strong> object which describes photo content to be shared&period;<&sol;li> &NewLine;<li>To share <strong><span style&equals;"color&colon; &num;008000&semi;">videos<&sol;span><&sol;strong>&comma; we need to create a <strong><span style&equals;"color&colon; &num;008000&semi;">ShareVideoContent<&sol;span><&sol;strong> object which Provides the interface for video content to be shared&period;<&sol;li> &NewLine;<li>The <strong><span style&equals;"color&colon; &num;008000&semi;">Share dialog<&sol;span><&sol;strong> switches to the native Facebook for Android app then returns control to your app after a post is published&period;<&sol;li> &NewLine;<li>The share dialog <span style&equals;"color&colon; &num;008000&semi;"><strong>canShow&lpar;&rpar;<&sol;strong><&sol;span> method returns <strong><span style&equals;"color&colon; &num;008000&semi;">true<&sol;span><&sol;strong> if the specified content type can be shown via the dialog&period;<&sol;li> &NewLine;<&sol;ul> &NewLine;<p>Given below is the screenshot of the app when you share a link to Facebook&period;<&sol;p> &NewLine;<p><img class&equals;"alignnone wp-image-3055" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2022&sol;02&sol;Screenshot&lowbar;20220208-152539-485x1024&period;png" alt&equals;"" width&equals;"312" height&equals;"658" &sol;><&sol;p> &NewLine;<p>Similarly&comma; when you share a photo and a video to Facebook&comma; it will look like this&colon;<&sol;p> &NewLine;<p><img class&equals;"alignnone wp-image-3056" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2022&sol;02&sol;Screenshot&lowbar;20220208-152618-485x1024&period;png" alt&equals;"" width&equals;"322" height&equals;"680" &sol;>    <img class&equals;"alignnone wp-image-3057" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2022&sol;02&sol;Screenshot&lowbar;20220208-152746-485x1024&period;png" alt&equals;"" width&equals;"318" height&equals;"670" &sol;>&NewLine;

Exit mobile version