android - Unable to fetch facebook profile picture, to insert in ImageView -
i unable fetch facebook profile picture, in order display in fragment. name, birthday , link fetched successfully, application stops working when try fetch profile picture. how rid of situation?
private facebookcallback<loginresult> callback=new facebookcallback<loginresult>() { @override public void onsuccess(loginresult loginresult) { accesstoken accesstoken=loginresult.getaccesstoken(); profile profile=profile.getcurrentprofile(); //displaymessage(profile); graphrequest request=graphrequest.newmerequest(accesstoken, new graphrequest.graphjsonobjectcallback() { @override public void oncompleted(jsonobject object, graphresponse response) { try{ tv1=(textview)getview().findviewbyid(r.id.name); tv1.settext("name : " + object.getstring("name")); tv2=(textview)getview().findviewbyid(r.id.birthday); tv2.settext("birthday : " + object.getstring("birthday")); tv3=(textview)getview().findviewbyid(r.id.id); tv3.settext("link : " + object.getstring("link")); string id=object.getstring("id"); bitmap mbitmap = getfacebookprofilepicture(id); img.setimagebitmap(mbitmap); /*toast.maketext(getactivity().getapplicationcontext(), object.getstring("name") + object.getstring("birthday") +object.getstring("link") , toast.length_short).show();*/ }catch (jsonexception e){ e.printstacktrace(); } } }); bundle parameters= new bundle(); parameters.putstring("fields","name,birthday,link"); request.setparameters(parameters); request.executeasync(); } @override public void oncancel() { } @override public void onerror(facebookexception error) { } }; public bitmap getfacebookprofilepicture(string userid) { bitmap bitmap = null; try { url imageurl = new url("https://graph.facebook.com/" + userid + "/picture?type=large"); bitmap = bitmapfactory.decodestream(imageurl.openconnection() .getinputstream()); } catch (malformedurlexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } return bitmap; }
everything can get.here code.
private callbackmanager callbackmanager; private loginbutton loginbutton; in oncreate(){ loginbutton = (loginbutton) findviewbyid(r.id.login_button); // don't forget give this. loginbutton.setreadpermissions(arrays.aslist("public_profile,email,user_birthday")); loginbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { getdetails(); } }); } private void getdetails() { //for facebook // facebooksdk.sdkinitialize(getapplicationcontext()); callbackmanager = callbackmanager.factory.create(); //register callback object facebook result loginmanager.getinstance().registercallback(callbackmanager, new facebookcallback<loginresult>() { @override public void onsuccess(loginresult loginresult) { graphrequest request = graphrequest.newmerequest(accesstoken.getcurrentaccesstoken(), new graphrequest.graphjsonobjectcallback() { @override public void oncompleted(jsonobject jsonobject, graphresponse graphresponse) { try { profile profile = profile.getcurrentprofile(); if (profile != null) { string facebook_id = profile.getid(); string f_name = profile.getfirstname(); string l_name = profile.getlastname(); profile_image = profile.getprofilepictureuri(400, 400).tostring(); } string email_id = jsonobject.getstring("email"); //email id } catch (jsonexception e) { logger.logerror(e); } } });
this surely works.
check whether using this. com.facebook.android:facebook-android-sdk:4.0.0
Comments
Post a Comment