2014-03-30 46 views
2

我想将facebook个人资料图片存储到解析用户表中。 当前我在试试这个:如何将facebook个人资料图片存储到Parse用户表中?

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(); 
    } 
} 

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

+0

你确定你使用的网址是正确的吗? –

+0

是的,您可以通过将您的ID放在user.getId()!的位置来查看它自己! – user3129550

+0

尝试和硬编码的FB ID而不是使用user.getId()(我怀疑你的用户为空或它返回无效的ID),并告诉我,如果它仍然是空的 – crazyPixel

回答

0

您的代码看起来正确除了,因为所有对graph.facebook.com的调用都应该通过SSL。切换httphttps,你应该很好去。

这是假设你在facebook回调中的用户对象不是ParseUser对象,因为FacebookUserObject.getId()ParseUserObject.getId()显然会返回不同的结果。如果您正在通过Request.newMeRequest()之类的请求进行Facebook回叫,则只需将协议更改为https即可运行。如果您不在Facebook回调中,并且用户对象是ParseUser对象,那么您将需要使用类似user.getString("facebookId")的地方,其中facebookId是您在上一步(可能是在初始注册期间)中保存的值。

+0

我修改了我的答案。除了您尝试对仅允许通过SSL连接的服务进行HTTP调用之外,您的代码是正确的。只需将您的http切换到https,其余的就可以工作。我只是对它进行了测试,并在稍作修改后生效。 –

0

你这样做的方式非常混乱,并可能在未来某个时候破坏......但是你有没有听说过毕加索。

确保您的网址是好的(logcat的),抢的Android毕加索库和改变这一点:

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

到这个(try/catch语句需要)

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

,你应该很好去!

相关问题