【问题标题】:How to store facebook profile picture into Parse user table?如何将 facebook 个人资料图片存储到 Parse 用户表中?
【发布时间】:2014-03-30 08:18:26
【问题描述】:

我想将 facebook 个人资料图片存储到解析用户表中。 目前我正在尝试这个:

URL img_value = null;
try {
    img_value = new URL("http://graph.facebook.com/"+user.getId()+"/picture?type=small");
} catch (MalformedURLException e){
    // TODO Auto-generated catch block
    e.printStackTrace();
}
try {
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);                                  
    Bitmap dp = BitmapFactory.decodeStream(img_value.openConnection().getInputStream());
    Log.i(IntegratingFacebook.TAG, "image retrieved from facebook");                                     
    // Save the user profile info in a user property
    ParseUser currentUser = ParseUser.getCurrentUser();     
    if(dp!=null){
       ParseFile saveImageFile= new ParseFile("profilePicture.jpg",compressAndConvertImageToByteFrom(dp));
       currentUser.put("profilePicture",saveImageFile);
    }
    currentUser.saveInBackground();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
}

网址是正确的。但是 dp 始终为空。 因此,图像永远不会存储在解析用户表中。 任何帮助表示赞赏。

【问题讨论】:

  • 你确定你使用的Url是正确的吗?
  • 是的,您可以通过将您的 id 代替 user.getId() 来自己查看!
  • 尝试硬编码一个 FB id 而不是使用 user.getId() (我怀疑你的用户是 null 或者它返回无效的 id)并告诉我它是否仍然是空的
  • 我试过了..dp 仍然是空的,而且 user.getId() 确实返回了一个有效的 id,因为我在我的应用程序的其他地方使用它并且它确实有效。

标签: android facebook parse-platform


【解决方案1】:

您的代码看起来是正确的除了,因为所有对graph.facebook.com 的调用都应该通过 SSL。将您的http 切换为https,您应该一切顺利。

这是假设您在 facebook 回调中,并且用户对象不是 ParseUser 对象,因为 FacebookUserObject.getId()ParseUserObject.getId() 显然会返回不同的结果。如果您正处于来自 Request.newMeRequest() 之类的请求的 Facebook 回调中,那么您的代码只需将协议更改为 https 即可工作。如果您不在 facebook 回调中并且用户对象是 ParseUser 对象,那么您将需要使用类似 user.getString("facebookId") 的东西,其中 facebookId 是您在上一步中保存的值(可能在初始注册期间) .

【讨论】:

  • 我修改了我的答案。您的代码是正确的,只是您尝试对仅允许通过 SSL 进行连接的服务进行 HTTP 调用。只需将您的 http 切换为 https,其余的就可以了。我刚刚对其进行了测试,经过轻微修改后它就可以工作了。
【解决方案2】:

你做这件事的方式非常混乱,将来可能会中断......但是你听说过毕加索吗?

确保您的 URL 正确(logcat),获取 Android 的 picasso 库并更改:

Bitmap dp = BitmapFactory.decodeStream(img_value.openConnection().getInputStream());

进入这个(需要尝试/捕获)

Bitmap dp = Picasso.with(getActivity()).load(URL).get();

你应该很高兴!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-06
    • 1970-01-01
    • 2017-02-23
    • 2014-12-20
    • 1970-01-01
    • 1970-01-01
    • 2017-09-30
    • 1970-01-01
    相关资源
    最近更新 更多