在导航抽屉活动中显示获取的个人资料图片 [英] Displaying fetched profile picture in Navigation drawer activity

查看:19
本文介绍了在导航抽屉活动中显示获取的个人资料图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望你能注意到下面这张图片中的教授图片 但随机的人只是硬编码了图像.我已经从 Facebook 获取了用户名、电子邮件 ID 和教授图片,并且我已经将用户名和电子邮件 ID 与导航抽屉活动挂钩,但我不知道如何显示我从 Facebook 获取的 prof_pic 我只是不知道没有如何分配它.让我知道是否有任何可能的方法来做到这一点.下面是 nav_header.xml

Hope you can notice a prof pic in this below Image but the random guy who did it just hard coded the Image. I have fetched the username, E-mail ID and prof pic from Facebook and I have hooked username and E-mail id with navigation drawer activity,but I don't know how to display the prof_pic which I fetched from Facebook I just don't no how to assign it. let me know if there is any possible way to do it.the below is the nav_header.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="@dimen/nav_header_height"
    android:background="@drawable/side_nav_bar"
    android:gravity="bottom"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

    <ImageView
        android:id="@+id/profpic"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        android:src="@android:drawable/sym_def_app_icon" />

</LinearLayout>

这是我需要从中获取 prof_pic 的 java 文件.java 类文件

and this is the java file where I need to fetch the prof_pic from .java class file

public class Testing2 extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_testing2);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        ////////Code is used to fetch the user name////////////
        Bundle b = getIntent().getExtras();


        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
        TextView facebookName = (TextView) navigationView.getHeaderView(0).findViewById(R.id.manner);
        TextView Email = (TextView) navigationView.getHeaderView(0).findViewById(R.id.email);
        ImageView profilePictureView = (ImageView) navigationView.getHeaderView(0).findViewById(R.id.profpic);

        ////////Code is used to display the user name after fetching it from other activity////////////
        facebookName.setText(b.getCharSequence("name"));
        Email.setText(b.getCharSequence("email"));
        profilePictureView.setProfileId();

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

  } 

这是我用来获取用户名、邮件 ID 和教授图片的 .java 文件

This is the .java file where I used to fetch the username,mail Id and prof pic

public class SelfTrail extends AppCompatActivity implements View.OnClickListener {

    private LoginButton btnLogin;
    TextView facebookName;
    TextView Email;
    Button button;
    private CallbackManager callbackManager;
    private ProfilePictureView profilePictureView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FacebookSdk.sdkInitialize(getApplicationContext());

        setContentView(R.layout.activity_self_trail);
        findAllViewsId();
        button.setOnClickListener(this);

        btnLogin = (LoginButton)findViewById(R.id.login_button);
        facebookName = (TextView)findViewById(R.id.name);
        Email = (TextView)findViewById(R.id.Email);
        profilePictureView = (ProfilePictureView)findViewById(R.id.image);


        btnLogin.setReadPermissions(Arrays.asList("public_profile, email"));
        callbackManager = CallbackManager.Factory.create();

        if(AccessToken.getCurrentAccessToken() != null){
            RequestData();
            facebookName.setVisibility(View.VISIBLE);
            Email.setVisibility(View.VISIBLE);
            profilePictureView.setVisibility(View.VISIBLE);
        }

        btnLogin.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {

            @Override
            public void onSuccess(LoginResult loginResult) {
                GraphRequest request = GraphRequest.newMeRequest(
                        loginResult.getAccessToken(),
                        new GraphRequest.GraphJSONObjectCallback() {

                            @Override
                            public void onCompleted(JSONObject object, GraphResponse response) {
                                Log.v("Main", response.toString());
                                setProfileToView(object);
                            }
                        });
                Bundle parameters = new Bundle();
                parameters.putString("fields", "id,name,email");
                request.setParameters(parameters);
                request.executeAsync();

            }

            @Override
            public void onCancel() {

            }

            @Override
            public void onError(FacebookException exception) {

            }
        });
       }




    private void findAllViewsId() {
        button = (Button) findViewById(R.id.next);
        facebookName = (TextView)findViewById(R.id.name);
        Email = (TextView)findViewById(R.id.Email);
        profilePictureView = (ProfilePictureView)findViewById(R.id.image);
    }

    @Override
    public void onClick(View v) {

        Intent intent = new Intent(getApplicationContext(), Testing2.class);
        //Create a bundle object
        Bundle b = new Bundle();

        //Inserts a String value into the mapping of this Bundle
        b.putString("name", facebookName.getText().toString());
        b.putString("email", Email.getText().toString());
        b.putString("image", profilePictureView.toString());

        //Add the bundle to the intent.
        intent.putExtras(b);

        //start the DisplayActivity
        startActivity(intent);
    }

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


    public void RequestData(){
        GraphRequest request = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
            @Override
            public void onCompleted(JSONObject jsonObject,GraphResponse response) {

                JSONObject json = response.getJSONObject();
                try {
                    if(json != null){
                        facebookName.setText(jsonObject.getString("name"));
                        Email.setText(jsonObject.getString("email"));
                        profilePictureView.setPresetSize(ProfilePictureView.NORMAL);
                        profilePictureView.setProfileId(jsonObject.getString("id"));
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
        Bundle parameters = new Bundle();
        parameters.putString("fields", "id,name,email");
        request.setParameters(parameters);
        request.executeAsync();
    }


    private void setProfileToView(JSONObject jsonObject) {
        try {
            facebookName.setText(jsonObject.getString("name"));
            Email.setText(jsonObject.getString("email"));

            profilePictureView.setPresetSize(ProfilePictureView.NORMAL);
            profilePictureView.setProfileId(jsonObject.getString("id"));

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

}

推荐答案

最好的方法是使用 Picasso Library.它会自动从后台的 url 获取图像并将其设置为 imageView.

The best way would be using Picasso Library. It automatically fetches image from url in background and sets it to the imageView.

将此添加到依赖项下的 build.gradle 文件

Add this to the build.gradle file under dependencies

          compile 'com.squareup.picasso:picasso:2.5.2'

然后在你的代码中而不是 profilePictureView.setProfileId();使用

And then in your code instead of profilePictureView.setProfileId(); use

          Picasso.with(this).load( "http://graph.facebook.com/"+userID+"/picture?type=small").into(profilePictureView)

其中 userID 是配置文件的使用 ID.

Where userID is the used id of the profile.

这篇关于在导航抽屉活动中显示获取的个人资料图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆