android - 从重定向的 URL 中检索位图

标签 android facebook-graph-api

我正在使用以下无效方法从 URL 获取位图图像:

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;
    }

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

最佳答案

为什么不直接请求图片 URL 并直接获取它呢?

代替 https://graph.facebook.com/[object id]/picture

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

关于android - 从重定向的 URL 中检索位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11662881/

相关文章:

android - NavigationDrawer - 以编程方式创建的子菜单在屏幕旋转后消失

java - 无法解析 secondary_activity Android Studio

android - Android 7 中的黑色相机预览

android - 显示 AutoCompleteTextView 下拉菜单时键盘闪烁

javascript - 使 Facebook 页面插件响应(到处搜索,找不到解决方案)

facebook-graph-api - 在 Facebook Graph API 中,获取用户电子邮件地址的 API 调用是什么

android - Facebook Graph API getCover 不再工作

android - ImageView 适合而不拉伸(stretch)图像

以 :0 结尾的 Facebook 固定链接

javascript - 从Facebook登录重定向后,在Adobe DPS叠加中进行 meteor 渲染