Android - get facebook profile picture

后端 未结 28 2323
花落未央
花落未央 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:41

    You are getting null because the call to URL.openConnection() (or any other mechanism to fetch the image) is asynchronous. It returns after your line: return bitmap;. Therefore bitmap is always null.

    I suggest using a callback instead.

    This is what i did:

    final AQuery androidQuery = new AQuery(this);
    
            AjaxCallback imageCallback = new AjaxCallback() {
    
                @Override
                public void callback(String url, byte[] avatar, AjaxStatus status) {
    
                    if (avatar != null) {
                        save(avatar);
                    } else {
                        Log.e(TAG, "Cannot fetch third party image. AjaxStatus: " + status.getError());
                    }
                }
    
            };
    
            androidQuery.ajax(imageUrl, byte[].class, imageCallback);
    

    Android query allows you to get the image in different formats (e.g. byte array, Bitmap, etc.). There are other libraries out there, but the idea is the same.

提交回复
热议问题