Java 检索Facebook个人资料图片
我的graphrequest似乎工作正常,我检索登录名的用户没有问题,获取他们的ID也没有任何问题。我正在尝试存储他们的个人资料图片,以便在我的应用程序中使用(通过将其下载为位图),但似乎无法成功下载。有人能告诉我我做错了什么吗Java 检索Facebook个人资料图片,java,android,facebook,facebook-graph-api,bitmap,Java,Android,Facebook,Facebook Graph Api,Bitmap,我的graphrequest似乎工作正常,我检索登录名的用户没有问题,获取他们的ID也没有任何问题。我正在尝试存储他们的个人资料图片,以便在我的应用程序中使用(通过将其下载为位图),但似乎无法成功下载。有人能告诉我我做错了什么吗 //Run the first time we log into Facebook //connects everything here private void firstTimeFBlogin() { GraphRequest
//Run the first time we log into Facebook
//connects everything here
private void firstTimeFBlogin() {
GraphRequest request = GraphRequest.newMeRequest(
AccessToken.getCurrentAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
Log.i("joe", "Heyyyyo");
try {
String userName = response.getJSONObject().getString("name");
String userID = response.getJSONObject().getString("id");
//String hi2 = response.getJSONObject().getString("first_name");
//String hi3 = response.getJSONObject().getString("gender");
final JSONObject mPicture = object.getJSONObject("picture");
final JSONObject mPictureData = mPicture.getJSONObject("data");
final String mImageUrl = mPictureData.getString("url");
Log.i("joe", "User's Facebook Name: " + userName);
Log.i("joe", ParseUser.getCurrentUser().getUsername());
Log.i("joe", mImageUrl);
Log.i("joe", userID);
ParseUser.getCurrentUser().put("name", userName);
ParseUser.getCurrentUser().put("iUrl", mImageUrl);
ParseUser.getCurrentUser().put("fbID", userID);
ParseUser.getCurrentUser().saveInBackground();
profilePictureRetriever(userID);
} catch (JSONException e) {
Log.i("joe", "Couldn't Succesfully retrieve the stuff...");
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,link,picture");
request.setParameters(parameters);
request.executeAsync();
}
public void profilePictureRetriever(String id) {
Log.i("joe", "Profile Picture Checker");
//Bitmap bitmap = getFacebookProfilePicture(id);
//this is here for test purposes
//tried just maually putting the url in..
Bitmap bm = DownloadImageBitmap("https://graph.facebook.com/849993771766163/picture?type=square");
}
public static Bitmap DownloadImageBitmap(String url) {
Bitmap bm = null;
try {
URL aURL = new URL(url);
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
bm = BitmapFactory.decodeStream(bis);
bis.close();
is.close();
} catch (IOException e) {
Log.e("IMAGE", "Error getting bitmap", e);
}
return bm;
}
}
有没有其他/更好的方法可以做到这一点
非常感谢 更新 在bundle中传递所需参数,因为
id、姓名、电子邮件、性别、生日、图片在下面的代码中传递
LoginManager.getInstance().registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
// App code
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,gender, birthday,picture");
request.setParameters(parameters);
request.executeAsync();
}
@Override
public void onCancel() {
Log.e(TAG, "Cancel");
}
@Override
public void onError(FacebookException exception) {
Log.e(TAG, "Error");
Log.e(TAG, exception.toString());
}
});
LoginManager.getInstance().registerCallback(callbackManager,
新建FacebookCallback(){
@凌驾
成功时公共无效(LoginResult LoginResult){
//应用程序代码
GraphRequest请求=GraphRequest.newmereRequest(
loginResult.getAccessToken(),
新建GraphRequest.GraphJSONObjectCallback(){
@凌驾
公共空间未完成(
JSONObject对象,
GraphResponse(反应){
}
});
Bundle参数=新Bundle();
参数.putString(“字段”、“id、姓名、电子邮件、性别、生日、图片”);
请求。设置参数(参数);
request.executeAsync();
}
@凌驾
公开作废{
Log.e(标记“取消”);
}
@凌驾
public void onError(facebook异常){
Log.e(标记“错误”);
Log.e(标记,exception.toString());
}
});
旧答案
打电话就可以了
graph.facebook.com/<FB UserId>/picture?type=large
graph.facebook.com//picture?type=large
更新
在bundle中传递所需参数,因为id、姓名、电子邮件、性别、生日、图片在下面的代码中传递
LoginManager.getInstance().registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
// App code
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,gender, birthday,picture");
request.setParameters(parameters);
request.executeAsync();
}
@Override
public void onCancel() {
Log.e(TAG, "Cancel");
}
@Override
public void onError(FacebookException exception) {
Log.e(TAG, "Error");
Log.e(TAG, exception.toString());
}
});
LoginManager.getInstance().registerCallback(callbackManager,
新建FacebookCallback(){
@凌驾
成功时公共无效(LoginResult LoginResult){
//应用程序代码
GraphRequest请求=GraphRequest.newmereRequest(
loginResult.getAccessToken(),
新建GraphRequest.GraphJSONObjectCallback(){
@凌驾
公共空间未完成(
JSONObject对象,
GraphResponse(反应){
}
});
Bundle参数=新Bundle();
参数.putString(“字段”、“id、姓名、电子邮件、性别、生日、图片”);
请求。设置参数(参数);
request.executeAsync();
}
@凌驾
公开作废{
Log.e(标记“取消”);
}
@凌驾
public void onError(facebook异常){
Log.e(标记“错误”);
Log.e(标记,exception.toString());
}
});
旧答案
打电话就可以了
graph.facebook.com/<FB UserId>/picture?type=large
graph.facebook.com//picture?type=large
我做过这样的事情:
首先,您需要调用graphrequestapi
以获取用户的所有详细信息,其中API
还提供当前配置文件图片的URL
Bundle params = new Bundle();
params.putString("fields", "id,email,gender,cover,picture.type(large)");
new GraphRequest(token, "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 = getFacebookProfilePicture(profilePicUrl);
// set profilePic bitmap to imageview
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}).executeAsync();
您可以使用这个方法,它从URL
返回用户的当前配置文件图片,我们从上面的graphrequestapi
获得该图片
public static Bitmap getFacebookProfilePicture(String url){
Bitmap bitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream());
return bitmap;
}
我希望有帮助 我做过这样的事情:
首先,您需要调用graphrequestapi
以获取用户的所有详细信息,其中API
还提供当前配置文件图片的URL
Bundle params = new Bundle();
params.putString("fields", "id,email,gender,cover,picture.type(large)");
new GraphRequest(token, "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 = getFacebookProfilePicture(profilePicUrl);
// set profilePic bitmap to imageview
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}).executeAsync();
您可以使用这个方法,它从URL
返回用户的当前配置文件图片,我们从上面的graphrequestapi
获得该图片
public static Bitmap getFacebookProfilePicture(String url){
Bitmap bitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream());
return bitmap;
}
我希望有帮助 登录成功后将放入以下代码。
GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(),new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object,GraphResponse response) {
Log.d("info",object.toString()+"");
String id = object.getString("id");
String profilePic ="http://graph.facebook.com/"+id+"/picture";
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,first_name,last_name,age_range,gender,locale,timezone,updated_time,verified,email");
request.setParameters(parameters);
request.executeAsync();
登录成功后将放入以下代码。
GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(),new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object,GraphResponse response) {
Log.d("info",object.toString()+"");
String id = object.getString("id");
String profilePic ="http://graph.facebook.com/"+id+"/picture";
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,first_name,last_name,age_range,gender,locale,timezone,updated_time,verified,email");
request.setParameters(parameters);
request.executeAsync();
嗯,在行中:Bitmap Bitmap=BitmapFactory.decodeStream(url.openConnection().getInputStream());我似乎找不到“openConnection()方法”;这是你写的还是在图书馆里的?是的,这个方法似乎无法工作,有什么我应该导入的吗?URL aURL=newurl(URL);URLConnection conn=aURL.openConnection();嗯,在行中:Bitmap Bitmap=BitmapFactory.decodeStream(url.openConnection().getInputStream());我似乎找不到“openConnection()方法”;这是你写的还是在图书馆里的?是的,这个方法似乎无法工作,有什么我应该导入的吗?URL aURL=newurl(URL);URLConnection conn=aURL.openConnection();不再可用不再可用