Al Hennessey
Al Hennessey

Reputation: 2445

Get users profile picture in circle image view

i have implemented Facebook login into my app and now want to get the users profile picture, i have done this but am now trying to turn it into a circle format using this library - https://github.com/vinc3m1/RoundedImageView

I am having trouble implementing the two together and am not sure the best way to go around it

Here is the java

final RoundedImageView profilePic = (RoundedImageView) rootView.findViewById(R.id.myProfilePic);
    GraphRequestAsyncTask request = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
        @Override
        public void onCompleted(JSONObject user, GraphResponse response) {
            if (user != null) {
                // set the profile picture using their Facebook ID
                profilePic.setProfileId(user.optString("id"));
            }
        }
    }).executeAsync();

And here is the xml

<com.makeramen.roundedimageview.RoundedImageView
        android:id="@+id/myProfilePic"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"

        android:layout_gravity="center_horizontal"
        android:padding="10dip"

        android:scaleType="center"
        app:riv_corner_radius="30dip"
        app:riv_border_width="3dip"
        app:riv_oval="false"
        app:riv_border_color="@color/material_white"
        />

I am getting error

Cannot resolve method 'SetProfileId'

I think the error is because I am not returning a proper bitmap in the graph request, but not sure how to solve this.

Upvotes: 1

Views: 1436

Answers (2)

Neeraj Kumar
Neeraj Kumar

Reputation: 943

You can achieve the same using picasso

Below is the code :

Picasso.with(context).load(imageURL)
            .error(drawable)
            .placeholder(drawable)
            .transform(new RoundedTransformation()).into(imgView);

public class RoundedTransformation implements Transformation {
@Override
public Bitmap transform(Bitmap source) {
    try {

        int size = Math.min(source.getWidth(), source.getHeight());

        int x = (source.getWidth() - size) / 2;
        int y = (source.getHeight() - size) / 2;

        Bitmap squaredBitmap = Bitmap
                .createBitmap(source, x, y, size, size);
        if (squaredBitmap != source) {
            source.recycle();
        }

        Bitmap bitmap = Bitmap.createBitmap(size, size, source.getConfig());

        Canvas canvas = new Canvas(bitmap);
        Paint paint = new Paint();
        BitmapShader shader = new BitmapShader(squaredBitmap,
                BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);
        paint.setShader(shader);
        paint.setAntiAlias(true);

        float r = size / 2f;
        canvas.drawCircle(r, r, r, paint);

        squaredBitmap.recycle();
        return bitmap;
    } catch (Exception e) {
        // TODO: handle exception
        if (BuildConfig.DEBUG)
            e.printStackTrace();
        return null;
    }

}

@Override
public String key() {
    return "circle";
}

}

Upvotes: 0

Franci Penov
Franci Penov

Reputation: 76001

Looking at RoundedImageView source I don't see any method called setprofileId(). You will need to make a request to Facebook Graph API to get the URL to the profile picture, and then either hand it to the view (if it has such method), or download it yourself and pass it as a Bitmap.

Upvotes: 1

Related Questions