Sharing Images on Facebook from Phimpme Android App

In Phimpme Android app, our next objective was to share the image to the Social media platform. As Facebook being the largest social media platform it is easier to reach many people through it. Below are the steps we followed to implement sharing an image on Facebook.

Step 1

First, we need to setup our android application development environment. We need to add Facebook SDK for android to the environment.

This can either be done by downloading Facebook SDK or directly adding it to the gradle.

In Phimpme we have used the latter(adding facebook sdk through gradle).

Add this to your build.gradle file before depencies:

repositories {
  mavenCentral() 
}

And add this in your dependencies:

compile 'com.facebook.android:facebook-android-sdk:4.23.0'

We, can use any sdk after 4+ but 4.23.0 is the latest when Phimpme Android was in development.

Step 2

For every application which is under development Facebook requires the user to create a Facebook App ID. This can be done by logging to https://developers.facebook.com/apps and then creating the app. This will generate an APP ID on the dashboard.

We need add this App ID to our android development environment. In Strings.xml (app/src/main/res/values/strings.xml.) Add a new String of the name facebook_app_id and the value should be the facebook app id which was generated in the https://developers.facebook.com/apps dashboard.

<string name="facebook_app_id">1920575401558694</string>

Step 3

Generating Key Hash and adding the hash key to the https://developers.facebook.com/apps. This Key Hash is required to communicate information between the app and Facebook.

To generate the Key Hash run the following command in the terminal in the development directory.

keytool -exportcert -alias <RELEASE_KEY_ALIAS> -keystore <RELEASE_KEY_PATH> | openssl sha1 -binary | openssl base64

Where release key should be replaced with app id and the path of the release key path. This will generate a string which we have to paste it to the https://developers.facebook.com/apps under the key hashes column.

Step 4

We need to take internet and Facebook provider permission in the manifest and :

Permission:

<uses-permission android:name="android.permission.INTERNET"/>

In place of APP_ID we need to add facebook app ID that was generated in https://developers.facebook.com/apps  

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

 

Step 5

After setting up Facebook integration we can send the Image. But to send the image we have to convert to Bitmap.

In Phimpme all the sharing function is established in Share Image Activity.    

   So, we need to convert the image to Bitmap:

BitmapFactory.Options bmOptions = new BitmapFactory.Options();
Bitmap image = BitmapFactory.decodeFile(saveFilePath, bmOptions);

saveFilePath is the path where the Edited Image is stored. BitmapFactory converts the file to Bitmap by using decodeFile() method.

Step 6

When share through Facebook button is pressed the image is converted to Bitmap and it is finally shared to the Facebook platform by using SharePhoto class:

SharePhoto photo = new SharePhoto.Builder()

       .setBitmap(image)

       .setCaption(sendMessage)

       .build();

SharePhotoContent content = new SharePhotoContent.Builder()

       .addPhoto(photo)

       .build();

ShareApi.share(content, null);

setCaption(): it sends text message with the image. In the case of Phimpme it gets the text message from the EditText stores to sendMessage.

The end result

When share through Facebook button is pressed, is checks if the user is already logged In or not. If the user is not logged In, the application will ask for Facebook ID and password.

After successfully logging In the application ask the permission to share the image.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.