无法解码流

时间:2014-08-12 10:48:36

标签: android facebook bitmap nullpointerexception

我想从Facebook下载图像头像(我已经设置了Facebook SDK和授权)。这是我的代码 -

if (Utils.isOnline(RegisterActivity.this)) 
{
    try 
    {
        // Download image
        image = BitmapFactory.decodeStream(imageUrl.openConnection().getInputStream());
        Log.d("Bitmap", "yes");
        // Saving images
        saveBitmapAsFile(image);
        return true;
    } 
    catch (IOException e) 
    {
        e.printStackTrace();
    }
} 
else 
{
    Toast.makeText(RegisterActivity.this, R.string.offlane, Toast.LENGTH_SHORT).show();
}

imageUrl - URL, - 像这样的链接:

"http://graph.facebook.com/" + facebookUserId + "/picture?type=large"

但是调用Bitmap.decodeStream后的位图图像仍为null,这样我就会捕获NullPointerException。请帮助我。

1 个答案:

答案 0 :(得分:1)

 HttpGet httpRequest = null;
                    try {
                        httpRequest = new HttpGet(new URL(imageUrl.toString()).toURI());
                    } catch (URISyntaxException e) {
                        e.printStackTrace();
                    }
                    HttpClient httpClient = new DefaultHttpClient();
                    HttpResponse response = (HttpResponse) httpClient.execute(httpRequest);
                    HttpEntity entity = response.getEntity();
                    BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
                    final long contentLength = bufHttpEntity.getContentLength();
                    Log.d("contentLength", String.valueOf(contentLength));
                    InputStream inputStream = bufHttpEntity.getContent();
                    imageBitmap = BitmapFactory.decodeStream(inputStream);
                    saveBitmapAsFile(imageBitmap);