0

我正在尝试使用 social-auth android 库从 Facebook 获取用户个人资料照片。

我登录成功,在 ResponseListener 的 onComplete 登录后获得用户信息,我获得了用户个人资料图像 url,但是当我将其设置为 ImageView 时,我的 ImageView 变为空白。

这是我的代码。

mSocialAdapter = new SocialAuthAdapter(new ResponseListener());
mSocialAdapter.authorize(this,Provider.FACEBOOK);

private final class ResponseListener implements DialogListener{

    @Override
    public void onBack() {
    }

    @Override
    public void onCancel() {
    }

    @Override
    public void onComplete(Bundle bundle) {
        mSocialAdapter.getUserProfileAsync(new SocialAuthListener<Profile>() {

            @Override
            public void onExecute(String arg0, Profile profile) {
                if(profile.getDisplayName() != null)
                Log.e("Display name", profile.getDisplayName());
                if(profile.getProfileImageURL() != null){
                Log.e("Profile Image Url", profile.getProfileImageURL());
                Picasso.with(RegisterProfileSetupActivity.this).load(profile.getProfileImageURL()).into(imgUserPhoto);
                //imgUserPhoto.setImageBitmap(loadImage(profile.getProfileImageURL()));

                }
            }

            @Override
            public void onError(SocialAuthError arg0) {
            }
        });

    }

    @Override
    public void onError(SocialAuthError arg0) {

    }

}

我也尝试这种方法来加载位图

public Bitmap loadImage(String url) {
    DefaultHttpClient client = new DefaultHttpClient();
    Bitmap bitmap = null;
    try {
        HttpResponse response = client.execute(new HttpGet(url));
        HttpEntity entity = response.getEntity();
        if(entity != null) {
            InputStream in = entity.getContent();
            bitmap = BitmapFactory.decodeStream(in);
        }
    }
    catch (Exception e) {
    }
    return bitmap;
}
4

1 回答 1

1

实际上,您需要在 AsyncTask 类中异步加载图像。这是代码。它工作正常。

private static Bitmap downloadBitmap(String url) {
        final AndroidHttpClient client = 
            AndroidHttpClient.newInstance("Android");
        final HttpGet request = new HttpGet(url);

        try {
            HttpResponse response = client.execute(request);
            final int statusCode = 
                response.getStatusLine().getStatusCode();

            if (statusCode != HttpStatus.SC_OK) {
                Header[] headers = response.getHeaders("Location");

                if (headers != null && headers.length != 0) {
                    String newUrl = 
                        headers[headers.length - 1].getValue();
                    // call again with new URL
                    return downloadBitmap(newUrl);
                } else {
                    return null;
                }
            }

            final HttpEntity entity = response.getEntity();
            if (entity != null) {
                InputStream inputStream = null;
                try {
                    inputStream = entity.getContent();                      

                    return BitmapFactory.decodeStream(inputStream);
                } finally {
                    if (inputStream != null) {
                        inputStream.close();  
                    }
                    entity.consumeContent();
                }
            }
        } catch (Exception e) {
            request.abort();
        } finally {
            if (client != null) {
                client.close();
            }
        }

        return null;
    }
    class LoadImageTask extends AsyncTask<String, Void, Bitmap>
    {

        @Override
        protected void onPreExecute() {

        }


        @Override
        protected Bitmap doInBackground(String... params) {

            Bitmap btn =  downloadBitmap(params[0]);

            return btn;
        }
        @Override
        protected void onPostExecute(Bitmap result) {
            imgProfileImage.setImageBitmap(result);
//          super.onPostExecute(result);
        }
    }

代替

//imgUserPhoto.setImageBitmap(loadImage(profile.getProfileImageURL()));

写下这一行

new LoadImageTask().execute(profile.getProfileImageURL());
于 2014-06-09T11:57:27.507 回答