0

I'm trying to apply the user's Facebook profile pic as the 'icon' property in a Marker. But for some unknown reason, I'm receiving a Nullpointer exception when doing so.

The syntax is correct, I just cut off some code just to show you the only bit that's affecting it (involved with the marker).

AccessToken accessToken = AccessToken.getCurrentAccessToken();
            MarkerOptions marker = new MarkerOptions().position(
                    new LatLng(point.latitude, point.longitude)).title("New Marker");
            globalMap.addMarker(marker
                    .position(point)
                    .icon(BitmapDescriptorFactory.fromBitmap(getFacebookProfilePicture(accessToken.getUserId())))
                    .anchor(0.5f, 1));
        }
    });
}

public static Bitmap getFacebookProfilePicture(String userID){
    Bitmap bitmap = null;
    try {
        URL imageURL = new URL("https://graph.facebook.com/" + userID + "/picture?type=large");
         bitmap = BitmapFactory.decodeStream(imageURL.openConnection().getInputStream());
    }catch(Exception e){

    }
    return bitmap;
}

I think it's this line that's giving me the problem but...

icon(BitmapDescriptorFactory.fromBitmap(getFacebookProfilePicture(accessToken.getUserId())))

...this is what the logcat is saying:

 java.lang.NullPointerException
                                                                 at maps.f.g.a(Unknown Source)
                                                                 at maps.ag.g$a.<init>(Unknown Source)
                                                                 at maps.ag.g.a(Unknown Source)
                                                                 at maps.ag.R.<init>(Unknown Source)
                                                                 at maps.ag.t.a(Unknown Source)
SaltySea
  • 700
  • 1
  • 7
  • 21

1 Answers1

1

Your mistake is that you're not establishing actual network connection. You should call URLConnection.Connect() method before loading user's profile image.

try {
    URL imageURL = new URL("https://graph.facebook.com/" + userID + "/picture?type=large");
    HttpURLConnection connection = (HttpURLConnection) imageUrl.openConnection();
    connection.setDoInput(true);
    connection.connect();
    bitmap = BitmapFactory.decodeStream(connection.getInputStream());
catch(IOException ex) {

}
floyd
  • 692
  • 8
  • 23
  • Oh, nice! For url.openConnection(); I assume you meant imageURL, not url. Also, should I wrap this in a try and catch? I have 3 warnings saying that it should handle IOException and MalformedURL. – SaltySea Jun 24 '16 at 20:56
  • **UPDATE** I tried that and wrapped it in a try and catch and I'm still getting the same problem :/ – SaltySea Jun 24 '16 at 21:02
  • hmm... Are you sure that you're using correct image url? – floyd Jun 24 '16 at 21:10
  • Yeah, I copied that some of that code from other classes that use the profile pic - and it works. Except in those classes I'm using Glide to load them. – SaltySea Jun 24 '16 at 21:15
  • try to use url that contains your access token: URL imageURL = new URL("https://graph.facebook.com/" + userID + "/picture?type=large&method=GET&access_token=" + yourAccessToken); – floyd Jun 24 '16 at 21:20
  • 1
    That's not working either. I think I might have found a longer way to do it with: http://stackoverflow.com/questions/13763545/android-maps-api-v2-with-custom-markers Gonna sleep then give that a shot. Thank you again <3 – SaltySea Jun 25 '16 at 05:49
  • 1
    You can also try to use Picassa Library to download image: http://square.github.io/picasso/, this library is very easy to use. – floyd Jun 25 '16 at 19:41
  • Yeah but I need it to be in a bitmap and Picasso doesn't have a tool that can convert to a bitmap. :( It's something with the BitmapFactory.decodeStream that's giving me the problem. The accessToken is working fine, adding the marker works it's just failing to get it from the URL (and the URL works fine as well) – SaltySea Jun 25 '16 at 22:36
  • here's an example of how you can download picture as bitmap using Picasso: http://stackoverflow.com/questions/24302431/how-to-load-a-bitmap-with-picasso-without-using-an-imageview – floyd Jun 25 '16 at 22:53
  • IT'S WORKING NOW, I USED PICASSO THANK YOU SO MUCH <3 – SaltySea Jun 27 '16 at 16:56