Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/facebook/9.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
Android 从Facebook Graph API获取用户图像_Android_Facebook_Facebook Graph Api - Fatal编程技术网

Android 从Facebook Graph API获取用户图像

Android 从Facebook Graph API获取用户图像,android,facebook,facebook-graph-api,Android,Facebook,Facebook Graph Api,我想在列表视图中显示用户配置文件图片。 当我试图从android调用graph api来检索图像时,总是会出现以下错误 java.io.IOException: Hostname <fbcdn-profile-a.akamaihd.net> was not verified at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.getSecureSocket(HttpConnectio

我想在列表视图中显示用户配置文件图片。 当我试图从android调用graph api来检索图像时,总是会出现以下错误

java.io.IOException: Hostname <fbcdn-profile-a.akamaihd.net> was not verified
    at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.getSecureSocket(HttpConnection.java:170)
    at org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnection$HttpsEngine.connect(HttpsURLConnection.java:398)
    at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.sendRequest(HttpURLConnection.java:1224)
    at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.doRequestInternal(HttpURLConnection.java:1558)
    at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.doRequest(HttpURLConnection.java:1551)
    at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1052)
    at org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnection.getInputStream(HttpsURLConnection.java:252)
    at com.facebook.android.Util.openUrl(Util.java:200)
    at com.facebook.android.Facebook.request(Facebook.java:559)
当我在浏览器中执行相同的调用时(https://graph.facebook.com//picture?access_token=),然后我得到一个像这样的url返回的图像
https://fbcdn-profile-a.akamaihd.net/...

图像以哪种格式发送给我?带有对图像的引用(url)的JSON

其中ID是您的个人资料ID之一

有关更多详细信息,请查看此


可以用另一种方式书写:

ImageView user_picture;
         ImageView userpicture = (ImageView)findViewById(R.id.userpicture);
         URL img_value = null;
         try {
            img_value = new URL("http://graph.facebook.com/"+"100004545962286"+"/picture?type=large");
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
         Bitmap mIcon1 = null;
        try {
            mIcon1 = BitmapFactory.decodeStream(img_value.openConnection().getInputStream());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
         userpicture.setImageBitmap(mIcon1);

我知道这个问题有点老了,但是现在有另一种方法可以很容易地获得用户的图片

首先,在xml布局中使用:

<com.facebook.widget.ProfilePictureView
        android:id="@+id/userImage"
        android:layout_width="69dp"
        android:layout_height="69dp"
        android:layout_gravity="center_horizontal" />

希望这能帮助别人。我也面临同样的问题,我得到了这个帖子,但现在是2011年。如今(2013年),做事的方式已经改变。

您可以通过使用ProfilePictureView而不是图像视图来完成:

<com.facebook.widget.ProfilePictureView
   android:id="@+id/friendProfilePicture"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:gravity="center_horizontal"
   android:padding="5sp"
   facebook:preset_size="small" />

希望有此帮助。

使用下面的代码,通过
Facebook Graph API
获取用户配置文件图像

     ImageView profileImage = (ImageView) findViewById(R.id.profileImage);
     Bundle params = new Bundle();
     params.putBoolean("redirect", false);
     params.putString("type", "large");
          new GraphRequest(
          AccessToken.getCurrentAccessToken(),
          "me/picture",
          params,
          HttpMethod.GET,
          new GraphRequest.Callback() {
          public void onCompleted(GraphResponse response) {
          try {
            String picUrlString = (String) response.getJSONObject().getJSONObject("data").get("url");
            Glide.with(getApplicationContext()).load(picUrlString).placeholder(R.drawable.ic_launcher).into(profileImage);
          } catch (JSONException | IOException e) {
            e.printStackTrace();
        }
    }
}
).executeAsync();    
更多信息请参考

获取用户图像的简单方法

String UserImageUrl="https://graph.facebook.com/" + facebookUser.getFacebookID() + "/picture?type=large"; 
Glide.with(getApplicationContext()).load(UserImageUrl).placeholder(R.drawable.ic_launcher).into(profileImage); 

有一种简单的方法可以为登录用户获取facebook用户图像

要使代码正常工作,请添加此导入语句:

import com.facebook.Profile;
请参见下面的示例(在这个示例中,我用于设置图像):

数字400和400分别是图像宽度和高度

private String facebookUser;


AccessToken token;
    token = AccessToken.getCurrentAccessToken();

    facebookUser = AccessToken.getCurrentAccessToken().getUserId();
    ProfilePictureView profilePictureView;
    profilePictureView = (ProfilePictureView) findViewById(R.id.facebookUser);
profilePictureView.setProfileId(facebookUser);
xml

希望有帮助

你好,文卡特什,这正是我要找的。Thanks@FelipeConde你不需要任何访问令牌。您可以在任何浏览器上使用此URL,并查看它是否适用于任何配置文件id@IncrediApp你是对的。当使用“我”而不是用户id时,您只需要该令牌。@Venky I获得下一个错误:“android.os.NetworkOnMainThreadException”。您是否已将此代码放入异步任务中?谢谢,这段代码将阻止主线程,直到下载完成。真的很糟糕,特别是如果你说其中30个请求是目前为止最简单的解决方案,但我认为该软件包已经移动到com.facebook.login.widget.ProfilePictureViewSeems,url有重定向,图像将为空。这是比使用httpclient或webclient下载用户图像更好的方法吗?哪条路更好更快?
     ImageView profileImage = (ImageView) findViewById(R.id.profileImage);
     Bundle params = new Bundle();
     params.putBoolean("redirect", false);
     params.putString("type", "large");
          new GraphRequest(
          AccessToken.getCurrentAccessToken(),
          "me/picture",
          params,
          HttpMethod.GET,
          new GraphRequest.Callback() {
          public void onCompleted(GraphResponse response) {
          try {
            String picUrlString = (String) response.getJSONObject().getJSONObject("data").get("url");
            Glide.with(getApplicationContext()).load(picUrlString).placeholder(R.drawable.ic_launcher).into(profileImage);
          } catch (JSONException | IOException e) {
            e.printStackTrace();
        }
    }
}
).executeAsync();    
String UserImageUrl="https://graph.facebook.com/" + facebookUser.getFacebookID() + "/picture?type=large"; 
Glide.with(getApplicationContext()).load(UserImageUrl).placeholder(R.drawable.ic_launcher).into(profileImage); 
import com.facebook.Profile;
Uri imageUri = Profile.getCurrentProfile().getProfilePictureUri(400, 400);
Picasso.with(this).load(imageUri).into(imageView);
private String facebookUser;


AccessToken token;
    token = AccessToken.getCurrentAccessToken();

    facebookUser = AccessToken.getCurrentAccessToken().getUserId();
    ProfilePictureView profilePictureView;
    profilePictureView = (ProfilePictureView) findViewById(R.id.facebookUser);
xml
<com.facebook.login.widget.ProfilePictureView
 android:id="@+id/facebookUser"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"></com.facebook.login.widget.ProfilePictureView>