2015-08-31 23 views

Antwort

48

Zuerst müssen Sie die GraphRequest API aufrufen, um alle Details des Benutzers zu erhalten, in denen die API auch die URL des aktuellen Profilbilds angibt.

Bundle params = new Bundle(); 
params.putString("fields", "id,email,gender,cover,picture.type(large)"); 
new GraphRequest(AccessToken.getCurrentAccessToken(), "me", params, HttpMethod.GET, 
     new GraphRequest.Callback() { 
      @Override 
      public void onCompleted(GraphResponse response) { 
       if (response != null) { 
        try { 
         JSONObject data = response.getJSONObject(); 
         if (data.has("picture")) { 
          String profilePicUrl = data.getJSONObject("picture").getJSONObject("data").getString("url"); 
          Bitmap profilePic= BitmapFactory.decodeStream(profilePicUrl .openConnection().getInputStream()); 
          mImageView.setBitmap(profilePic); 
         } 
        } catch (Exception e) { 
         e.printStackTrace(); 
        } 
       } 
      } 
}).executeAsync(); 
+0

Dies wirft Fehler für mich, weil decodeStream nach einer URL statt einer Zeichenfolge sucht - mache ich etwas falsch? – ntgCleaner

+0

haben Sie versuchen BitmapFactory.decodeStream (url.op enConnection(). getInputStream()); ?? – Rajesh

+1

Ich habe Ihren genauen Text dort verwendet, habe '' getFacebookProfilePicture() 'außerhalb von onCreate platziert und zeigt' .openConnection() 'als nicht aufgelöst an. Nicht sicher, was ich falsch mache – ntgCleaner

0

einfach diese URL aufrufen:

graph.facebook.com/<facebook_user_id>/picture?type=large 

Art können große, normal oder klein sein.

Ein anderer Weg ist ProfilePictureView

<com.facebook.login.widget.ProfilePictureView 
    android:id="@+id/image" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerInParent="true" 
    facebook:preset_size="small"/> 

Danach Sie setzen in können Code wie facebook id verwenden

profilePictureView.setProfileId(facebookUserId); 
+0

danke das funktioniert.Können Sie mir sagen, wie man diesen Link mit Graph Anfrage API verwenden ein kleines Code-Snippet –

3

Wenn Sie wirklich großes Bild wollen, müssten Sie spec. mindestens eine Bildgröße - z.

String profileImg = "https://graph.facebook.com/" + loginResult.getAccessToken().getUserId() + "/picture?type=large&width=1080"; 

Sie können aber auch beide Größen angeben (add & height = some_val), aber dann wird facebook Profil Bild zuschneiden.

14

Von letzten sdk 4.5.0

String url; 
Bundle parametersPicture = new Bundle(); 
parametersPicture.putString("fields", "picture.width(150).height(150)"); 

GraphResponse lResponsePicture = new GraphRequest(AccessToken.getCurrentAccessToken(), "/me/", 
         parametersPicture, null).executeAndWait(); 
if (lResponsePicture != null && lResponsePicture.getError() == null && 
          lResponsePicture.getJSONObject() != null) { 
    url = lResponsePicture.getJSONObject().getJSONObject("picture") 
           .getJSONObject("data").getString("url"); 
} 
1
try { 
String fbId="970463683015249"; 
URL fb_url = new URL("http://graph.facebook.com/"+fbId+"/picture?type=small");//small | noraml | large 
HttpsURLConnection conn1 = (HttpsURLConnection) fb_url.openConnection(); 
HttpsURLConnection.setFollowRedirects(true); 
conn1.setInstanceFollowRedirects(true); 
Bitmap fb_img = BitmapFactory.decodeStream(conn1.getInputStream()); 
}catch (Exception ex) { 
    ex.printStackTrace(); 
    } 
2

protected void rajendra (LoginButton login_button) {

login_button.registerCallback(callbackManager, new FacebookCallback<LoginResult>() { 
     @Override 
     public void onSuccess(LoginResult login_result) { 
      GraphRequest request = GraphRequest.newMeRequest(
        login_result.getAccessToken(), 
        new GraphRequest.GraphJSONObjectCallback() { 
         @Override 
         public void onCompleted(
           JSONObject object, 
           GraphResponse response) { 

          response.getError(); 

          try { 
           if (android.os.Build.VERSION.SDK_INT > 9) { 
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
            StrictMode.setThreadPolicy(policy); 
            String profilePicUrl = object.getJSONObject("picture").getJSONObject("data").getString("url"); 

            URL fb_url = new URL(profilePicUrl);//small | noraml | large 
            HttpsURLConnection conn1 = (HttpsURLConnection) fb_url.openConnection(); 
            HttpsURLConnection.setFollowRedirects(true); 
            conn1.setInstanceFollowRedirects(true); 
            Bitmap fb_img = BitmapFactory.decodeStream(conn1.getInputStream()); 
            image.setImageBitmap(fb_img); 
           } 
          }catch (Exception ex) { 
           ex.printStackTrace(); 
          } 
         } 
        }); 
      Bundle parameters = new Bundle(); 
      parameters.putString("fields", "id,picture"); 
      request.setParameters(parameters); 
      request.executeAsync(); 
     } 
1

Get Bild von Facebook

String image_url = "http://graph.facebook.com/" + Profile.getCurrentProfile().getId() + "/picture?type=large"; 
Glide.with(activity) 
    .load(image_url) 
    .into(imageView); 

Abhängigkeit

compile 'com.github.bumptech.glide:glide:4.1.1' 
0

facebook Bild

https://graph.facebook.com/ "+ facebookUid +"/Bild?height = 9999 & Redirect = 0"

Google Bild

String googleEmailName googleEmail.substring = (0, googleEmailName.indexOf ("@"))

http://picasaweb.google.com/data/entry/api/user/ "+ googleEmailName +"? alt = json

+0

Sie sollten etwas schreiben, um zu erklären, was Sie tun. – trungduc

+0

fügen Sie bitte eine Beschreibung hinzu. –

+0

Danke .i erklären –

Verwandte Themen