How to get Facebook profile picture from Android App? -
i have created simple test app on android "login facebook".
this code:
public class mainactivity extends appcompatactivity { private textview info; private loginbutton loginbutton; private callbackmanager callbackmanager; string email; string birthday; string name; string gender; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); facebooksdk.sdkinitialize(getapplicationcontext()); setcontentview(r.layout.activity_main); callbackmanager = callbackmanager.factory.create(); info = (textview)findviewbyid(r.id.fb_text); loginbutton = (loginbutton)findviewbyid(r.id.fb_login); loginbutton.setreadpermissions(arrays.aslist("public_profile", "email", "user_birthday")); loginbutton.registercallback(callbackmanager, new facebookcallback<loginresult>() { @override public void onsuccess(loginresult loginresult) { info.settext( "user id: " + loginresult.getaccesstoken().getuserid() + "\n" + "auth token: " + loginresult.getaccesstoken().gettoken() ); graphrequest request = graphrequest.newmerequest( loginresult.getaccesstoken(), new graphrequest.graphjsonobjectcallback() { @override public void oncompleted(jsonobject object, graphresponse response) { log.v("loginactivity", response.tostring()); // application code try { email = object.getstring("email"); birthday = object.getstring("birthday"); // 01/31/1980 format name = object.getstring("name"); gender = object.getstring("gender"); } catch (jsonexception e) { e.printstacktrace(); } info.append( "user name: " + name + "\n" + "birthday: " + birthday + "\n" + "gender: " + gender + "\n" + "email: " + email ); } }); bundle parameters = new bundle(); parameters.putstring("fields", "id,name,email,gender,birthday"); request.setparameters(parameters); request.executeasync(); } @override public void oncancel() { info.settext("login cancelled"); } @override public void onerror(facebookexception e) { info.settext("login failed"); } }); } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); callbackmanager.onactivityresult(requestcode, resultcode, data); } }
what should profile picture of user , show activity?
just add following parameter in existing code
parameters.putstring("fields", "id,name,email,gender,birthday,picture.type(large)");
picture.type(large) return url profile picture
by way can url jsonobject returned facebook this:
profilepicurl = object.getjsonobject("picture").getjsonobject("data").getstring("url");
Comments
Post a Comment