如何在不返回null的情况下获取facebook个人资料图片?

时间:2015-06-11 21:29:30

标签: android facebook bitmap

我想获取facebook个人资料图片,但它返回Bitmap null。为什么? 调试我得到这个: 位图b = getUserPic(用户ID); b:null byte [] image = convertBitmapToByteArray(b); image:null

public void displayWelcomeMessage(Profile profile){
    if (profile != null){
        //mTextDetails.setText("Welcome "+ profile.getName());

        databaseHelper = new Database(getActivity());
        String name = profile.getName();
        String idade = profile.getLastName();
        String userIds = profile.getId();

        Bitmap b = getUserPic(userIds);

        byte[] image = convertBitmapToByteArray(b);

         long id = databaseHelper.insertData(name, idade, image);
         if(id < 0){
         Message.message(getActivity(), "Unsuccessful");
         } else{
         Message.message(getActivity(), "Successfully Inserted a Row");
         }
    }
}
 public Bitmap getUserPic(String userID) {
 String imageURL;
 Bitmap bitmap = null;
 //Log.d(TAG, "Loading Picture");
 imageURL = "https://graph.facebook.com/"+userID+"/picture?type=small";
 try {
 bitmap = BitmapFactory.decodeStream((InputStream)new URL(imageURL).getContent());
 } catch (Exception e) {
 Log.d("TAG", "Loading Picture FAILED");
 e.printStackTrace();
 }
 return bitmap;
 }

2 个答案:

答案 0 :(得分:1)

看看我的回答

我认为这是因为Facebook在您拨打https://graph.facebook.com/"+userID+"/picture?type=small时会发送重定向。

答案 1 :(得分:0)

试试这个:

URL url = new URL(imageURL);
Bitmap image = BitmapFactory.decodeStream(url.openConnection().getInputStream());