0

I want to get the facebook profile picture, but it returns me Bitmap null. Why? The debug I get this: Bitmap b = getUserPic (user IDs); 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;
 }
Vitor
  • 51
  • 6

2 Answers2

1

Have a look at my answer at

I think this is because Facebook is sending a redirect when you call https://graph.facebook.com/"+userID+"/picture?type=small.

Community
  • 1
  • 1
Tobi
  • 31,405
  • 8
  • 58
  • 90
  • i am using the same url with picasso and is working String url = "https://graph.facebook.com/" + userId + "/picture?width=300&height=300"; Picasso.with(context).load(url).placeholder(R.drawable.def_profile_pic).into(avatar); – Panayiotis Irakleous Jun 12 '15 at 07:41
  • I don't now about Picasso, but I'd guess it handles redirects transparently, and your custom implementation doesn't – Tobi Jun 12 '15 at 07:50
0

Try this:

URL url = new URL(imageURL);
Bitmap image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
Panayiotis Irakleous
  • 2,696
  • 1
  • 23
  • 36