尝试在Android中获取用户的Facebook个人资料图片时出现白色问号

时间:2015-04-04 04:48:48

标签: java android facebook

我将app-scoped-id存储在我的数据库中,希望通过图谱API从中获取个人资料图片。

此代码:

String url = "https://graph.facebook.com/" + mate.getFacebookId() + "/picture?type=large";

    DefaultHttpClient httpClient = new DefaultHttpClient();

    HttpGet httpGet = new HttpGet(url);
    HttpResponse response = null;
    HttpEntity entity = null;

    try {

        response = httpClient.execute(httpGet);

    } catch (IOException e) {
        e.printStackTrace();
    }

    if (response != null) {
        entity = response.getEntity();
    }

    byte[] data = null;
    try {
        data = EntityUtils.toByteArray(entity);
    } catch (IOException e) {
        e.printStackTrace();
    }

    Bitmap bitmap = null;
    if (data != null) {

        bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);


    }

    return bitmap;

...不起作用并返回一个白色问号,为什么?

网址' https://graph.facebook.com/800397776713349/picture?type=normal'在chrome中工作得很好。

1 个答案:

答案 0 :(得分:1)

此行上的分号丢失:

String url = "https://graph.facebook.com/" + mate.getFacebookId() + "/picture?type=large"

看起来您使用的是无效的Facebook ID。检查mate.getFacebookId()是否返回有效 ID。当我使用有效的ID时,我能够在AsyncTask中成功返回图像:

在主线程内使用(onCreate,onActivityCreated等):

GetImage retrieve = new GetImage((ImageView)findViewById(R.id.yourimageview));
retrieve.execute();

的getImage:

class GetImage extends AsyncTask<String, Void, Bitmap>
{
    private ImageView view;

    public GetImage(ImageView view)
    {
        this.view = view;
    }

    @Override
    protected Bitmap doInBackground(String... params)
    {
        //Your exact code goes here
    }

    @Override
    protected void onPostExecute(Bitmap b) 
    {
        view.setImageBitmap(b);
    }

}