How to integrate Facebook Api in Android


 

Before you start to generate Facebook App id. Take a look on facebook development.

Facebook allows you to obtain a token to access Facebook’s API on behalf of someone using your app. You can use this feature in place of building your own account system or to add Facebook services to your existing accounts.The Facebook SDK for Android provides methods to implement Facebook Login for your app. And to access this feature into your app you need facebook app id.

Now start to generate facebbok id , you should need a facebook account as a developer or you can use your personal account also.

Step1: Goto facebook developer site and login.

Here is facebook developer link https://developers.facebook.com/apps open this and login to developer account.

How to integrate Facebook Api in Android

Step2: click on “Add a New App” and than select your app platform, here we are using Android so click on “Android”.

How to integrate Facebook Api in Android

Step3: Now click on “Create App Id” enter your app details and finally click on “Create App Id”

How to integrate Facebook Api in Android

Step4: Your Facebook App Id is generated, copy this app id and used for your app development.

How to integrate Facebook Api in Android

Step5: If you need facebook app secret key than click on “show” button and you will get your secret key.

Step6: Goto to https://developers.facebook.com/docs/android and download facebook api.

Step6: After download facebook sdk extract it. You will get facebook api project import it to you workspace.

How to integrate Facebook Api in Android

Step7: Create a project name ImageSharing and add facebooksdk lib into your project from properties. And write this codes.

Write this code in MainActivity.java

Write  this code in MainActivity.java
package com.example.imagesharing;
import java.io.File;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.Signature;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;

import com.facebook.UiLifecycleHelper;
import com.facebook.widget.FacebookDialog;

public class MainActivity extends Activity {
	private UiLifecycleHelper uiHelper;
	FacebookDialog shareDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		showHashKey(this);
		uiHelper = new UiLifecycleHelper(this, null);
		uiHelper.onCreate(savedInstanceState);

		// to share photo=================

		Bitmap bm = BitmapFactory.decodeResource(getResources(),
				R.drawable.ic_launcher);
		List<Bitmap> image = new ArrayList<Bitmap>();
		image.add(bm);
		if (FacebookDialog.canPresentShareDialog(getApplicationContext(),
				FacebookDialog.ShareDialogFeature.PHOTOS)) {
			FacebookDialog shareDialog = new FacebookDialog.PhotoShareDialogBuilder(
					this).addPhotos(image).build();
			uiHelper.trackPendingDialogCall(shareDialog.present());
		} else {
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		uiHelper.onActivityResult(requestCode, resultCode, data,
				new FacebookDialog.Callback() {
					@Override
					public void onError(FacebookDialog.PendingCall pendingCall,
							Exception error, Bundle data) {
						Log.e("Activity",
								String.format("Error: %s", error.toString()));
					}

					@Override
					public void onComplete(
							FacebookDialog.PendingCall pendingCall, Bundle data) {
						Log.i("Activity", "Success!");
					}
				});
	}

	@Override
	protected void onResume() {
		super.onResume();
		uiHelper.onResume();
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		uiHelper.onSaveInstanceState(outState);
	}

	@Override
	public void onPause() {
		super.onPause();
		uiHelper.onPause();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		uiHelper.onDestroy();
	}
//this function is to get hash key of your project.
//you can check it in log.
	public static void showHashKey(Context context) {
		try {
			PackageInfo info = context.getPackageManager().getPackageInfo(
					"com.example.imagesharing",
					PackageManager.GET_SIGNATURES); // Your package name here
			for (Signature signature : info.signatures) {
				MessageDigest md = MessageDigest.getInstance("SHA");
				md.update(signature.toByteArray());
				Log.i("KeyHash:",
						Base64.encodeToString(md.digest(), Base64.DEFAULT));
			}
		} catch (NameNotFoundException e) {
		} catch (NoSuchAlgorithmException e) {
		}
	}

}
Open string.xml in value folder and write this line
<string name="app_id">1533995260210845</string>
Now finally in your manifest.xml write this codes.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.imagesharing"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.imagesharing.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.facebook.LoginActivity"
            android:theme="@android:style/Theme.Translucent.NoTitleBar" />
        <meta-data android:name="com.facebook.sdk.ApplicationId"
            android:value="@string/app_id" />
        <provider android:authorities="com.facebook.app.NativeAppCallContentProvider1533995260210845"
                  android:name="com.facebook.NativeAppCallContentProvider"
                  android:exported="true"/>
    </application>

</manifest>

Run your app now you will get direct facebook page to share image on facebook.com

Posted in All Tutorials, Android Tutorials | Tagged | Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Powered By: wpnova.com