Android - get facebook profile picture

后端 未结 28 2314
花落未央
花落未央 2020-12-12 15:13

I don\'t know why, but I am always getting null when I try to get the profile picture of the user. Do I need to set some specific permissions to get access?

Below is

相关标签:
28条回答
  • 2020-12-12 15:44
    Bundle bundle = new Bundle();
    
    bundle.putString ("fields", "full_picture,message");
    
    new GraphRequest(
                    AccessToken.getCurrentAccessToken(),
                    "{page-id}/feed",
                    bundle,
                    HttpMethod.GET,
                    new GraphRequest.Callback() {
                        public void onCompleted(GraphResponse response) {
                /* handle the result */
                            Log.e("TAG", response.toString());
                        }
                    }
            ).executeAsync();
    
    0 讨论(0)
  • 2020-12-12 15:44

    I have done this way:

    Get Bitmap from Image Url of Facebook:

    String imageUrl = "http://graph.facebook.com/103407310026838/picture?type=large&width=1000&height=1000";
    
    Bitmap bitmap = getFacebookProfilePicture(imageUrl);
    

    Function for Bitmap:

    private Bitmap getFacebookProfilePicture(String url){
        Bitmap bitmap = null;
        HttpGet httpRequest = new HttpGet(URI.create(url));
        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse mResponse;
        try {
        mResponse = (HttpResponse) httpclient.execute(httpRequest);
        HttpEntity entity = mResponse.getEntity();
            BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
            bitmap = BitmapFactory.decodeStream(bufHttpEntity.getContent());
            httpRequest.abort();
        }
        catch(Exception e){
            e.printStackTrace();
        }
       return bitmap;
     }
    

    It's Done.

    0 讨论(0)
  • 2020-12-12 15:46

    Best way to Get Profile Picture URL

    int dimensionPixelSize = getResources().getDimensionPixelSize(com.facebook.R.dimen.com_facebook_profilepictureview_preset_size_large);
    Uri profilePictureUri= Profile.getCurrentProfile().getProfilePictureUri(dimensionPixelSize , dimensionPixelSize);
    

    or

    Uri profilePictureUri = ImageRequest.getProfilePictureUri(Profile.getCurrentProfile().getId(), dimensionPixelSize , dimensionPixelSize );
    

    Use Glide to show Image

    Glide.with(this).load(profilePictureUri)
                    .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                    .into(profilePictureView);
    

    No more hard-coded string

    0 讨论(0)
  • 2020-12-12 15:46
       public static Bitmap getFacebookProfilePicture(String userID)
                    throws SocketException, SocketTimeoutException,
                    MalformedURLException, IOException, Exception {
                String imageURL;
                Bitmap bitmap = null;
                imageURL = "http://graph.facebook.com/" + userID
                        + "/picture?type=large";
    
                 URL url1 = new URL(imageURL);
                    HttpURLConnection ucon1 = (HttpURLConnection) url1.openConnection();
                    ucon1.setInstanceFollowRedirects(false);
                    URL secondURL1 = new URL(ucon1.getHeaderField("Location"));
                InputStream in = (InputStream) new URL(imageURL).getContent();
                bitmap = BitmapFactory.decodeStream(in);
                return bitmap;
            }
    

    use this code.....

    0 讨论(0)
  • 2020-12-12 15:48

    On my graph api was not working because of the question mark

    If you are using 1 pram after picture it would be

    picture&type=large
    

    for two params we would use the question mark

    picture?type=large&redirect=false
    

    Hope it Helps!

    0 讨论(0)
  • 2020-12-12 15:49

    Url seems OK.

    So problem is with your connection. Does URL.getContent() really return stream? Because if BitmapFactory gets null it also returns null.

    Try this:

    Bitmap bitmap = null;
    URL url = new URL(http://graph.facebook.com/"+userID+"/picture?type=large);
    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
    try {
       InputStream in = new BufferedInputStream(urlConnection.getInputStream());
       bitmap = BitmapFactory.decodeStream(in);
    }
    finally {
          urlConnection.disconnect();
    }
    
    0 讨论(0)
提交回复
热议问题