2012-07-26 39 views
3

我使用下面的空隙的方法來得到一個URL的位圖圖像:從重定向的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; 
    } 

最常用的網址我用這種方法餵養是這樣的: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並獲取位圖文件?

回答

-1

需要追加類型= URL中大到得到的圖片...

e.g下面的代碼演示瞭如何獲取對imageview的...

ImageView的user_picture?;

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

URL img_value = null;

img_value = new URL(「http://graph.facebook.com/」+ id +「/ picture?type = large」);

位圖mIcon1 = BitmapFactory.decodeStream(img_value.openConnection()。getInputStream());

userpicture.setImageBitmap(mIcon1);

嘗試在URL中添加?type = large並讓我知道您的結果。

+0

的這一翻譯還不行。 – phreakhead 2013-05-09 02:56:51

0

爲什麼不直接請求圖片網址並直接獲取?

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

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

+0

'https://graph.facebook.com/ [object id]?fields = picture'返回'JSON'。因此,我不會直接提取它,而是先解析「JSON」以獲取確切的URL。不錯的建議,但! :) – 2012-07-26 10:26:27