在导览栏活动中显示提取的个人资料图片 [英] Displaying fetched profile picture in Navigation drawer activity

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

问题描述

希望您可以注意到以下图片,但是随机的人只是硬编码了图像。我从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>

这是我需要从
.java类中获取prof_pic的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();

  } 

这是我曾经获取的.java文件用户名,邮件ID和prof pic

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

}


推荐答案

最好的方法是使用毕加索图书馆。它会自动从后台的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天全站免登陆