Android facebook连接设置个人资料图片

时间:2016-09-09 14:20:26

标签: android facebook

我已将facebook connect整合到我的应用中。现在使用我的代码我只使用用户名。如何检索FB的图像并将其设置为Circle ImageView? 这是我连接和检索名称的代码:

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();
}



}

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

尝试下载并对个人资料进行四舍五入,

从Facebook获取img并下载

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

 downloadImg(new OnImageDownloaded() {
            @Override
            public void onResponce(Bitmap bitmap) {
                if (bitmap != null) {
                imageview.setImageBitmap(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);
}

获取圆角位图

 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;
}