2016-06-11 120 views
-1

我無法獲取Facebook個人資料圖片,以便將其顯示在我的片段中。名稱,生日和鏈接已成功獲取,但是當我試圖獲取個人資料圖片時,應用程序停止工作。如何擺脫這種情況?無法獲取Facebook個人資料圖片,插入ImageView

private FacebookCallback<LoginResult> callback=new FacebookCallback<LoginResult>() { 



    @Override 
    public void onSuccess(LoginResult loginResult) { 
     AccessToken accessToken=loginResult.getAccessToken(); 
     Profile profile=Profile.getCurrentProfile(); 
     //DisplayMessage(profile); 

     GraphRequest request=GraphRequest.newMeRequest(accessToken, new GraphRequest.GraphJSONObjectCallback() { 
      @Override 
      public void onCompleted(JSONObject object, GraphResponse response) { 

       try{ 

        tv1=(TextView)getView().findViewById(R.id.name); 
        tv1.setText("Name : " + object.getString("name")); 

        tv2=(TextView)getView().findViewById(R.id.birthday); 
        tv2.setText("BirthDay : " + object.getString("birthday")); 

        tv3=(TextView)getView().findViewById(R.id.id); 
        tv3.setText("link : " + object.getString("link")); 

        String id=object.getString("id"); 
        Bitmap mBitmap = getFacebookProfilePicture(id); 
        img.setImageBitmap(mBitmap); 

        /*Toast.makeText(getActivity().getApplicationContext(), object.getString("name") + object.getString("birthday") +object.getString("link") 
          , Toast.LENGTH_SHORT).show();*/ 

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




      } 
     }); 

     Bundle parameters= new Bundle(); 
     parameters.putString("fields","name,birthday,link"); 
     request.setParameters(parameters); 
     request.executeAsync(); 



    } 

    @Override 
    public void onCancel() { 

    } 

    @Override 
    public void onError(FacebookException error) { 

    } 
}; 


public Bitmap getFacebookProfilePicture(String userID) { 

    Bitmap bitmap = null; 

    try { 
     URL imageURL = new URL("https://graph.facebook.com/" + userID 
       + "/picture?type=large"); 
     bitmap = BitmapFactory.decodeStream(imageURL.openConnection() 
       .getInputStream()); 

    } catch (MalformedURLException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    return bitmap; 

} 

回答

2

你可以得到的一切。這裏是代碼。

private CallbackManager callbackManager; 
private LoginButton loginButton; 
    in OnCreate(){ 
    loginButton = (LoginButton) findViewById(R.id.login_button); 
    // don't forget to give this. 
    loginButton.setReadPermissions(Arrays.asList("public_profile,email,user_birthday")); 
     loginButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
     getDetails(); 
     } 
    }); 
     } 
    private void getDetails() { 
//for facebook 
// FacebookSdk.sdkInitialize(getApplicationContext()); 
callbackManager = CallbackManager.Factory.create(); 
//register callback object for facebook result 
    LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() { 
      @Override 
      public void onSuccess(LoginResult loginResult) { 

       GraphRequest request = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(), 
         new GraphRequest.GraphJSONObjectCallback() { 
          @Override 
          public void onCompleted(JSONObject jsonObject, GraphResponse graphResponse) { 
           try { 
            Profile profile = Profile.getCurrentProfile(); 
            if (profile != null) { 
             String facebook_id = profile.getId(); 
             String f_name = profile.getFirstName(); 
             String l_name = profile.getLastName(); 
             profile_image = profile.getProfilePictureUri(400, 400).toString(); 
            } 
            String email_id = jsonObject.getString("email"); //email id 
           } catch (JSONException e) { 
            Logger.logError(e); 
           } 
          } 

         }); 

這肯定有效。

檢查你是否使用這個。 com.facebook.android:facebook-android-sdk:4.0.0

0

對於一個快速的解決方案,你可以得到的用戶資料圖片:

String profilePictureUrl = "https://graph.facebook.com/"+userID+"/picture?width=500&height=500"; 

並與您的圖像加載庫下載到您的圖像視圖。

編輯:

這是Android(舊的,但好) https://github.com/nostra13/Android-Universal-Image-Loader

使用方便簡單ImageLoading庫: 將它添加到您的項目作爲一個依賴的gradle。

ImageLoader imageLoader = ImageLoader.getInstance(); 
imageLoader.displayImage(profilePictureUrl, yourImageView); 
+0

如何將圖像加載程序庫下載到圖像視圖中。我是一名初學者。你能否詳細說明一下。謝謝。 –

0

我用這樣的:

public void getUserDetailsFromFB(AccessToken accessToken) { 

GraphRequest req=GraphRequest.newMeRequest(accessToken, new GraphRequest.GraphJSONObjectCallback() { 
    @Override 
    public void onCompleted(JSONObject object, GraphResponse response) { 
     Toast.makeText(getApplicationContext(),"graph request completed",Toast.LENGTH_SHORT).show(); 
     try{ 
      String email = object.getString("email"); 
      String birthday = object.getString("birthday"); 
      String gender = object.getString("gender"); 
      String name = object.getString("name"); 
      String id = object.getString("id"); 
      String photourl =object.getJSONObject("picture").getJSONObject("data").getString("url"); 

     }catch (JSONException e) 
     { 
      Toast.makeText(getApplicationContext(),"graph request error : "+e.getMessage(),Toast.LENGTH_SHORT).show(); 

     } 

    } 
}); 
Bundle parameters = new Bundle(); 
parameters.putString("fields", "id,name,email,gender,birthday,picture.type(large)"); 
req.setParameters(parameters); 
req.executeAsync(); 
} 

然後使用Picasso像:Picasso.with(context).load(photourl).into(imageView);

Json格式在這裏:

{ 
    "id": "10204968538960870", 
    "name": "Yasin Kaçmaz", 
    "email": "[email protected]", 
    "gender": "male", 
    "birthday": "12/06/1994", 
    "picture": { 
    "data": { 
     "is_silhouette": false, 
     "url": "https://scontent.xx.fbcdn.net/v/t1.0..." 
    } 
    } 
} 

你可以嘗試Graph Api看到例如Json格式在:Graph Api Explorer

相關問題