Retrieve profile picture using Facebook SDK 3.0 for Android

前端 未结 6 1696
Happy的楠姐
Happy的楠姐 2021-02-19 03:24

I\'ve following problem with Facebook SDK 3.0 for Android. I wanna get my (and my friends) profile picture without using their ProfilePictureView widget, so if I use Graph Explo

6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-19 04:10

    As far as I know, it's not possible to get the image directly using the facebook graph API, because their API is designed to only accept JSON responses. It seems weird that they would allow a request that results in a non-JSON response, and especially strange that they would put that request in their official documentation *(https://developers.facebook.com/docs/graph-api/reference/user/picture/).

    I am using the built in Android DefaultHttpClient library.

    private Bitmap downloadImage(url) {
        Bitmap image = null;
        DefaultHttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet(imageUrl);
        try {
            HttpResponse response = client.execute(request);
            HttpEntity entity = response.getEntity();
    
            int imageLength = (int)(entity.getContentLength());
    
            InputStream is = entity.getContent();
    
            byte[] imageBlob = new byte[imageLength];
    
            int bytesRead = 0;
    
            // Pull the image's byte array
    
            while (bytesRead < imageLength) {
                int n = is.read(imageBlob, bytesRead, imageLength - bytesRead);
                bytesRead= n;
            }
    
            image = BitmapFactory.decodeByteArray(imageBlob, 0, imageBlob.length);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return image;
    }
    

提交回复
热议问题