Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在导航抽屉活动中显示提取的配置文件图片_Java_Android_Facebook_Android Studio_Navigation Drawer - Fatal编程技术网

Java 在导航抽屉活动中显示提取的配置文件图片

Java 在导航抽屉活动中显示提取的配置文件图片,java,android,facebook,android-studio,navigation-drawer,Java,Android,Facebook,Android Studio,Navigation Drawer,希望你能注意到下图中的一张教授图片,但随机的家伙只是把图片硬编码了。我已经从Facebook获取了用户名、电子邮件ID和prof pic,我已经将用户名和电子邮件ID与导航抽屉活动挂钩,但我不知道如何显示从Facebook获取的prof_pic,我只是不知道如何分配它。如果有任何可能的方法,请告诉我。下面是nav_header.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" a

希望你能注意到下图中的一张教授图片,但随机的家伙只是把图片硬编码了。我已经从Facebook获取了用户名、电子邮件ID和prof pic,我已经将用户名和电子邮件ID与导航抽屉活动挂钩,但我不知道如何显示从Facebook获取的prof_pic,我只是不知道如何分配它。如果有任何可能的方法,请告诉我。下面是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文件,我在其中获取用户名、邮件Id和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();
        }
    }

}
public类SelfTrail扩展了AppCompatActivity实现了View.OnClickListener{
私人登录按钮;
TextView facebookName;
TextView电子邮件;
按钮;
私人CallbackManager CallbackManager;
私人档案PictureView档案PictureView;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
sdkinInitialize(getApplicationContext());
setContentView(R.layout.activity\u self\u trail);
findAllViewsId();
setOnClickListener(此);
btnLogin=(LoginButton)findViewById(R.id.login_按钮);
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){
请求数据();
facebookName.setVisibility(View.VISIBLE);
Email.setVisibility(View.VISIBLE);
profilePictureView.setVisibility(View.VISIBLE);
}
registerCallback(callbackManager,newfacebookcallback()){
@凌驾
成功时公共无效(LoginResult LoginResult){
GraphRequest请求=GraphRequest.newmereRequest(
loginResult.getAccessToken(),
新建GraphRequest.GraphJSONObjectCallback(){
@凌驾
未完成公共无效(JSONObject对象,GraphResponse响应){
Log.v(“Main”,response.toString());
setProfileToView(对象);
}
});
Bundle参数=新Bundle();
参数.putString(“字段”、“id、名称、电子邮件”);
请求。设置参数(参数);
request.executeAsync();
}
@凌驾
公开作废{
}
@凌驾
public void onError(facebook异常){
}
});
}
私有void findAllViewsId(){
按钮=(按钮)findViewById(R.id.next);
facebookName=(TextView)findViewById(R.id.name);
Email=(TextView)findViewById(R.id.Email);
profilePictureView=(profilePictureView)findViewById(R.id.image);
}
@凌驾
公共void onClick(视图v){
Intent Intent=新的Intent(getApplicationContext(),Testing2.class);
//创建一个bundle对象
Bundle b=新Bundle();
//将字符串值插入此捆绑包的映射中
b、 putString(“name”,facebookName.getText().toString());
b、 putString(“email”,email.getText().toString());
b、 putString(“image”,profilePictureView.toString());
//将bundle添加到intent中。
意向.附加条款(b);
//启动DisplayActivity
星触觉(意向);
}
@凌驾
受保护的void onActivityResult(int请求代码、int结果代码、意图数据){
super.onActivityResult(请求代码、结果代码、数据);
callbackManager.onActivityResult(请求代码、结果代码、数据);
}
public void RequestData(){
GraphRequest request=GraphRequest.NewMereRequest(AccessToken.getCurrentAccessToken(),new GraphRequest.GraphJSONObjectCallback()){
@凌驾
未完成公共无效(JSONObject JSONObject,GraphResponse响应){
JSONObject json=response.getJSONObject();
试一试{
if(json!=null){
setText(jsonObject.getString(“名称”);
setText(jsonObject.getString(“电子邮件”));
profilePictureView.setPresetSize(profilePictureView.NORMAL);
profilePictureView.setProfileId(jsonObject.getString(“id”);
}
}捕获(JSONException e){
e、 printStackTrace();
}
}
});
Bundle参数=新Bundle();
参数.putString(“字段”、“id、名称、电子邮件”);
请求。设置参数(参数);
request.executeAsync();
}
私有void setProfileToView(JSONObject JSONObject){
试一试{
setText(jsonObject.getString(“名称”);
setText(jsonObject.getString(“电子邮件”));
profilePictureView.setPresetSize(profilePictureView.NORMAL);
profilePictureView.setProfileId(jsonObject.getString(“id”);
}捕获(JSONException e){
e、 printStackTrace();
}
}
}

最好的方法是使用毕加索图书馆。它在后台自动从url获取图像,并将其设置为imageView

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

          compile 'com.squareup.picasso:picasso:2.5.2'
然后在代码中,而不是在profilePictureView.setProfileId()中;使用


其中userID是配置文件的已用id

我不知道这是怎么回事:

profilePictureView.setProfileId()

但要设置图像,只需使用:

profilePictureView.setImageBitmap(在此处传递图像位图)          compile 'com.squareup.picasso:picasso:2.5.2'
          Picasso.with(this).load( "http://graph.facebook.com/"+userID+"/picture?type=small").into(profilePictureView)
/**
 * Function loads the users facebook profile pic
 * 
 * @param userID
 */
public Bitmap getUserPic(String userID) {
    String imageURL;
    Bitmap bitmap = null;
    Log.d(TAG, "Loading Picture");
    imageURL = "http://graph.facebook.com/"+userID+"/picture?type=small";
    try {
        bitmap = BitmapFactory.decodeStream((InputStream)new URL(imageURL).getContent());
    } catch (Exception e) {
        Log.d("TAG", "Loading Picture FAILED");
        e.printStackTrace();
    }
    return bitmap;
}
Picasso.with(context)
       .load(url)
       .placeholder(R.drawable.loading_img)
       .error(R.drawable.error_img)
       .into(profilePictureView);
Picasso.with(context)
   .load(url)
   .placeholder(R.drawable.loading_img)
   .error(R.drawable.error_img)
   .into(profilePictureView);