APPGIS APPGIS - 3 months ago 18
Android Question

Android facebook connect set Profile Picture

i have integrate facebook connect into my app. Now with my code i take only user name. How can i retrive picture image of FB and set it into Circle ImageView?
This is my code to connect and retrive name:

public class facebookFragment extends Fragment {

private EditText mTextDetails;
private TextView nome;
private ImageView profile_pic;
private CallbackManager mCallbackManager;
private AccessTokenTracker mTokenTracker;
private ProfileTracker mProfileTracker;
private FacebookCallback<LoginResult> mFacebookCallback = new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
Log.d("VIVZ", "onSuccess");
AccessToken accessToken = loginResult.getAccessToken();
Profile profile = Profile.getCurrentProfile();
mTextDetails.setText(constructWelcomeMessage(profile));

}


@Override
public void onCancel() {
Log.d("VIVZ", "onCancel");
}

@Override
public void onError(FacebookException e) {
Log.d("VIVZ", "onError " + e);
}
};


public facebookFragment() {
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

mCallbackManager = CallbackManager.Factory.create();
setupTokenTracker();
setupProfileTracker();

mTokenTracker.startTracking();
mProfileTracker.startTracking();
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.facebook_connect, container, false);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
setupTextDetails(view);
setupLoginButton(view);
}

@Override
public void onResume() {
super.onResume();
Profile profile = Profile.getCurrentProfile();
mTextDetails.setText(constructWelcomeMessage(profile));


}

@Override
public void onStop() {
super.onStop();
mTokenTracker.stopTracking();
mProfileTracker.stopTracking();
}

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

private void setupTextDetails(View view) {
mTextDetails = (EditText) view.findViewById(R.id.editText_nome);
nome = (TextView) view.findViewById(R.id.nome);
profile_pic = (ImageView) view.findViewById(R.id.profile_pic);
}

private void setupTokenTracker() {
mTokenTracker = new AccessTokenTracker() {
@Override
protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken currentAccessToken) {
Log.d("VIVZ", "" + currentAccessToken);
}
};
}

private void setupProfileTracker() {
mProfileTracker = new ProfileTracker() {
@Override
protected void onCurrentProfileChanged(Profile oldProfile, Profile currentProfile) {
Log.d("VIVZ", "" + currentProfile);
mTextDetails.setText(constructWelcomeMessage(currentProfile));
nome.setText(constructWelcomeMessage(currentProfile));
}
};
}

private void setupLoginButton(View view) {
LoginButton mButtonLogin = (LoginButton) view.findViewById(R.id.login_button_fb);
mButtonLogin.setFragment(this);
mButtonLogin.setReadPermissions("user_friends");
mButtonLogin.registerCallback(mCallbackManager, mFacebookCallback);
}

private String constructWelcomeMessage(Profile profile) {
StringBuffer stringBuffer = new StringBuffer();
if (profile != null) {
stringBuffer.append(profile.getName());
}
return stringBuffer.toString();
}



}


Thanks for any help

Answer

Try this to download and make the profile rounded,

to get the img from facebook and download it

 //                                             img size
 String myPicURL = profile.getProfilePictureUri(200, 200).toString();

 downloadImg(new OnImageDownloaded() {
            @Override
            public void onResponce(Bitmap bitmap) {
                if (bitmap != null) {
                imageview.getRoundedBitmap(bitmap);
                } else {
                    ...                      
                }

            }
        }).execute(myPicURL);



        public AsyncTask<String, Void, Bitmap> downloadImg(final OnImageDownloaded myInterface) {
            return new AsyncTask<String, Void, Bitmap>() {

               @Override
               protected Bitmap doInBackground(String... params) {
                  try {
                        // Download Image from URL
                        InputStream input = new java.net.URL(params[0]).openStream();
                        // Decode Bitmap
                        return BitmapFactory.decodeStream(input);
                    } catch (IOException e) {
                        e.printStackTrace();
                        return null;
                    } catch (NullPointerException npe) {
                        npe.printStackTrace();
                        return null;
                  }
             }

              @Override
              protected void onPostExecute(Bitmap result) {
                   myInterface.onResponce(result);
              }
        };
    }

private interface OnImageDownloaded {
    void onResponce(Bitmap bitmap);
}

to get the rounded bitmap

 public Bitmap getRoundedBitmap(Bitmap bitmap){
    Bitmap circleBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    BitmapShader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
    Paint paint = new Paint();
    paint.setShader(shader);
    paint.setAntiAlias(true);
    Canvas c = new Canvas(circleBitmap);
    c.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2, bitmap.getWidth() / 2, paint);
    return circleBitmap;
}