从重定向的URL中检索位图

时间:2012-07-26 05:25:30

标签: android facebook-graph-api

我使用以下void方法从URL获取Bitmap图像:

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

        try {
            HttpResponse response = client.execute(getRequest);
            final int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode != HttpStatus.SC_OK) { 
                Log.w("ImageDownloader", "Error " + statusCode + " while retrieving bitmap from " + url); 
                return null;
            }

            final HttpEntity entity = response.getEntity();
            if (entity != null) {
                InputStream inputStream = null;
                try {
                    inputStream = entity.getContent(); 
                    final Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                    return bitmap;
                } finally {
                    if (inputStream != null) {
                        inputStream.close();  
                    }
                    entity.consumeContent();
                }
            }
        } catch (Exception e) {
            // Could provide a more explicit error message for IOException or IllegalStateException
            getRequest.abort();
            Log.e("ImageDownloader", "Error while retrieving bitmap from " + url);
        } finally {
            if (client != null) {
                client.close();
            }
        }
        return null;
    }

我使用此方法提供的最常见网址是:http://graph.facebook.com/+FACEBOOK_USER_ID+/picture,但此网址重定向到http://profile.ak.fbcdn.net/static-ak/rsrc.php/v2/y9/r/xxxxx.gif。这就是为什么我没有得到Bitmap图像,我得到的是与302重定向相关的错误。 如何处理重定向的URL,并获取位图文件?

2 个答案:

答案 0 :(得分:0)

为什么不直接请求图片网址并直接获取?

而不是https://graph.facebook.com/[object id]/picture

请求 https://graph.facebook.com/[object id]?fields=picture

答案 1 :(得分:-1)

您需要在网址中附加?type = large才能获得图片...

例如,以下代码显示了如何进入imageview ...

  

ImageView user_picture;

     

userpicture =(ImageView的)findViewById(R.id.userpicture);

     

网址img_value = null;

     

img_value = new   URL( “http://graph.facebook.com/” + ID + “?/画面类型=大”);

     

位图mIcon1 =   BitmapFactory.decodeStream(img_value.openConnection()的getInputStream());

     

userpicture.setImageBitmap(mIcon1);

尝试在网址中添加?type = large ,并告诉我您的结果。