In order to integrate facebook into your android application to share images and data using Facebook SDK, you have to follow these steps.
1. Install openssl if not installed. The link is Win64 OpenSSL v1.0.2a Light at the moment. If this link becomes obsolete google openssl. Install openSSL.
2. Use the following command to get hashkey.
The KeyAlias value should be the same key alias you use when generating signed apk. And the keystore value is the same you use in signed apk wizard as well.
Here is the view from Android Studio.
"C:\Program Files\Java\jre1.8.0_31\bin\keytool" -exportcert -alias KeyAlias -keystore C:\Users\OZAN_2\AndroidStudioProjects\YourProject\keyStore.jks | C:\OpenSSL-Win64\bin\openssl sha1 -binary | C:\OpenSSL-Win64\bin\openssl base64
After you enter your password, the result will be a 28 character hashkey. Save it somewhere for later use.
3. Open https://developers.facebook.com/ click MyApps -> Add a new app to add your application to facebook. Fill the form and submit. The important parts are these fields:
Display Name: This should be the same as "<application android:label" value defined in your projects AndroidManifest.xml.
Google Play Package Name: This should be the same as "<manifest package=" value defined in your AndroidManifest.xml.
Class Name: Should be your activity name it is like "<activity android:name=".MainActivity"" in your AndroidManifest.xml, but you should use the class name without dot (in this case only "MainActivity").
Key Hashes: Here enter your key hash you generated in previous step.
Also note that you will get an App ID value, save it for later use.
4. After you added your application to facebook, open strings.xml in your project and make sure you have these lines:
<string name="app_name">ImageUpload</string>
<string name="facebook_app_id">9988776655443322</string>
Note that facebook_app_id value should contain the App ID you get in previous step.
5. Here are the steps to import Facebook SDK to your project:
- Download the Facebook SDK from this link: https://developers.facebook.com/docs/android
- Extract it in your project.
- Click "Import Module" from the File menu.
- Select source path of the module as the "facebook" directory inside the unzipped archive.
- Right click on your project and select Open Module Settings, Click on
+button and select Module Dependency. In the new window select:facebook.
6. Add these to your AndroidManifest.xml
<activity android:name="com.facebook.FacebookActivity"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:label="@string/app_name" />
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>
<meta-data android:name="com.facebook.sdk.ApplicationName" android:value="@string/app_name"/>
<provider android:authorities="com.facebook.app.FacebookContentProvider9988776655443322"
android:name="com.facebook.FacebookContentProvider"
android:exported="true" />
Change the end of this part FacebookContentProvider9988776655443322 so that it contains your app id instead of 9988776655443322.
7. For the android code side you can also follow the instructions in the link https://developers.facebook.com/docs/sharing/android
simply you can share picture using this code:
ShareDialog facebookDialog; // global
facebookDialog = new ShareDialog(this); // put this code to onCreate
SharePhoto photo = new SharePhoto.Builder()
.setBitmap(bitmap)
.build();
SharePhotoContent content = new SharePhotoContent.Builder()
.addPhoto(photo)
.build();
facebookDialog.show(content);
Without Facebook App Installed
If you don't have Facebook installed on your android phone, then you can follow the strategy of showing the general share to screen which will open a window containing all applications that can receive an image to process.
Here is the code:
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/png");
Uri screenshotUri = Uri.parse(path);
intent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
intent = Intent.createChooser(intent, "Share image using");
startActivity(intent);
Screen shot of "share to" screen (it is actually on my Blackberry z10 running android apk) in Turkish language.


