2014-03-30 44 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(); 

,你應該很好去!

相關問題