Facebook Profile picture in android
Step 1 : Do all actions mentioned in link for fb login https://developers.facebook.com/docs/facebook-login/android. (Ex: sha1 key,release key,etc..,)
Step 2 : create MainActivity like below attached find it.
Step 3 : create layout file like below attached find it.
Step 5 : Run execute and watch the output in layout. Over.
Step 6 : Comment this tutorial below or write what you want.
Step 7: useful code for facebook integration.
Step 2 :
public class MainActivity extends FragmentActivity {
CallbackManager callbackManager;
TextView tv;
ProfilePictureView profilePictureView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.tv);
profilePictureView = (ProfilePictureView) findViewById(R.id.image);
LoginButton loginButton = (LoginButton) findViewById(R.id.login_button);
loginButton.setReadPermissions("email");
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
callToast("Login success!");
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
tv.setText(object.toString());
try {
String id = String.valueOf(object.getString("id"));
callToast("id is::"+id);
profilePictureView.setProfileId(id);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,link,email,picture");
request.setParameters(parameters);
request.executeAsync();
}
@Override
public void onCancel() {
callToast("Login Cancel!");
}
@Override
public void onError(FacebookException error) {
callToast("Login Error!");
}
});
}
private void callToast(String s) {
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:facebook="http://schemas.android.com/apk/res-auto"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="mycompany.com.facebookintegration.MainActivity">
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textIsSelectable="true"
android:text="Hello World!" />
<com.facebook.login.widget.ProfilePictureView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
<com.facebook.login.widget.LoginButton
android:id="@+id/login_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
android:layout_marginBottom="30dp" />
</RelativeLayout>
Useful code:
mainfest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="mycompany.com.facebookintegration">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data android:name="com.facebook.sdk.ApplicationId"
android:value="@string/facebook_app_id"/>
<activity android:name="com.facebook.FacebookActivity"
android:configChanges=
"keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:label="@string/app_name" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
build.gradle(Module app):
apply plugin: 'com.android.application'
android {
compileSdkVersion 24
buildToolsVersion "24.0.0"
defaultConfig {
applicationId "mycompany.com.facebookintegration"
minSdkVersion 15
targetSdkVersion 24
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
repositories {
mavenCentral()
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.0.0'
compile 'com.facebook.android:facebook-android-sdk:4.17.0'
}
FacebookIntegrationApplication.java
public class FacebookIntegrationApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
FacebookSdk.sdkInitialize(getApplicationContext());
AppEventsLogger.activateApp(this);
}
}
strings.xml
<resources>
<string name="app_name">FacebookIntegration</string>
<string name="facebook_app_id">354644***912746</string>
</resources>
Get facebook profile using Graph API:
the following code taken from rajesh from http://stackoverflow.com/questions/19855072/android-get-facebook-profile-picture
try { JSONObject data = response.getJSONObject(); if (data.has("picture")) { String profilePicUrl = data.getJSONObject("picture").getJSONObject("data").getString("url"); Bitmap profilePic = BitmapFactory.decodeStream(profilePicUrl.openConnection().getInputStream()); // set profilePic bitmap to imageview } } catch (Exception e) { e.printStackTrace(); }