Pull facebook profile image to a panel

Asked

Viewed 101 times

0

package com.didasko.eduardo.tender;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.facebook.AccessToken;
import com.facebook.FacebookSdk;
import com.facebook.GraphRequest;
import com.facebook.GraphResponse;
import com.facebook.HttpMethod;
import com.squareup.picasso.Picasso;
import org.json.JSONObject;

import de.hdodenhof.circleimageview.CircleImageView;
import static com.didasko.eduardo.tender.R.id.profile_pic;

public class TelaPerfil extends AppCompatActivity {


    Button btnSair;
    ImageView adReceita;
    ImageView verMreceitas;


    ImageView user_picture;

    JSONObject response, profile_pic_data, profile_pic_url;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tela_perfil);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        FacebookSdk.sdkInitialize(getApplicationContext());





        btnSair = (Button) findViewById(R.id.btnSair);
        btnSair.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent1 = new Intent(TelaPerfil.this, MainActivity.class);
                startActivity(intent1);
                finish();
            }
        });

        adReceita = (ImageView) findViewById(R.id.adReceita);
        adReceita.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent1 = new Intent(TelaPerfil.this, NovaReceita.class);
                startActivity(intent1);
                finish();
            }
        });

        verMreceitas = (ImageView) findViewById(R.id.verMreceitas);
        verMreceitas.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent1 = new Intent(TelaPerfil.this, MinhasReceitas.class);
                startActivity(intent1);
                finish();
            }
        });



        new GraphRequest(
                AccessToken.getCurrentAccessToken(),
                "/{user-id}/picture",
                null,
                HttpMethod.GET,
                new GraphRequest.Callback() {
                    public void onCompleted(GraphResponse response) {

                        user_picture = (CircleImageView)findViewById(profile_pic);

                    }
                }
        ).executeAsync();


        final Intent intent = getIntent();
        String jsondata = intent.getStringExtra("jsondata");

        setUserProfile(jsondata);


        TextView user_name = (TextView)findViewById(R.id.nome);
        user_name.setText(Prefs.getNome(this));



    }

    public  void  setUserProfile(String jsondata){

        try {
            response = new JSONObject(jsondata);
            profile_pic_data = new JSONObject(response.get("picture").toString());
            profile_pic_url = new JSONObject(profile_pic_data.getString("data"));

            Picasso.with(this).load(profile_pic_url.getString("url"))
                    .into(user_picture);
        } catch (Exception e){
            e.printStackTrace();
        }

    }



}

I’m using the method Graph Request, but this not pulling the profile image, the name it pulls, already the image remains in the same blank, I’m doing something wrong? Something is missing?

OBS: This is not my main activity, it is a page that I want to display the name and image of the facebook user profile.

  • You are probably making an exception. The setUserProfile() method may be called before the request is completed.

  • Would you have any suggestions so I can improve the code?

  • See if this helps you http://answall.com/questions/90416/como-pega-foto-do-perfil-do-facebook/90836#90836

  • @Developmentdmidia call the method setUserProfile() inside onCompleted(). This is necessary because the request is asynchronous (see executeAsync()).

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.