2011-04-05 21 views
0

我在獲取圖像格式Internet時遇到問題。 我有自定義佈局圖像視圖和文本視圖的列表視圖。 在我的情況下,我正在使用給定的打擊功能獲取圖像形式的互聯網,但是當大圖像來發生位圖大小異常當獲取圖像格式Internet時,會發生位圖大小超過異常

private static InputStream fetch(String urlString) 
      throws MalformedURLException, IOException { 
     DefaultHttpClient httpClient = new DefaultHttpClient(); 
     HttpGet request = new HttpGet(urlString); 
     HttpResponse response = httpClient.execute(request); 
     return response.getEntity().getContent(); 
    } 

    Resources res; 

    // *********** Function For Fetching Image form Internet 
    public static Drawable LoadImage(String URL, BitmapFactory.Options options) { 

     try { 

      if (fetch(URL) != null) { 
       try { 
        InputStream obj = fetch(URL); 
        bmp = BitmapFactory.decodeStream(obj, null, options); 
        obj.reset(); 

       } catch (Exception e) { 
       } 
       Drawable drawable = new BitmapDrawable(bmp); 
       return drawable; 
      } else { 
       bitmap = BitmapFactory.decodeResource(
         TennisAppActivity.mContext.getResources(), 
         R.drawable.profileplaceholder); 
       Drawable drawable = new BitmapDrawable(bitmap); 
       return drawable; 
      } 
     } catch (Exception e) { 
      Log.e("GUIMethodClasas", "fetchDrawable failed", e); 
      return null; 
     } 

    } 

我使用上述代碼從互聯網獲取圖像。 請任何朋友儘快給我一個解決方案。

+0

請定義一個「大圖」。尺寸?尺寸?堆棧跟蹤? – WarrenFaith 2011-04-05 12:08:27

+0

這適用於我假設的一些圖像,但只與大圖像崩潰?我有一些類似的代碼與圖像大小調整,以防止類似的錯誤,當我從工作中,我會張貼從畫廊選擇大型圖像。 – DeliveryNinja 2011-04-05 12:14:56

回答

0

使用BitmapFactory選項並設置比默認值更高的採樣大小,以便消耗更少的內存並獲得更小的圖像,更易於由應用程序處理。

你可以這樣做是這樣的:

BitmapFactory.Options options=new BitmapFactory.Options(); 
options.inSampleSize = 4; //or any number you like. A power of 2 is recommended. 

然後,你可以通過這些選項,你的功能,這樣當你解碼流使用它們。

您提到的模糊是對圖像進行下采樣的成本。如果您不減少樣本數量,那麼當您從流中獲取樣本時,將無法縮小樣本數量。

+0

我給了BitmapFactory.Options = 5的值,然後問題解決了,但圖像更藍。但我想要沒有藍色的圖像。 – DynamicMind 2011-04-06 12:20:42

+0

我發佈了一個使用BitmapFactory選項的適當方法的代碼示例,解釋模糊是縮減採樣的結果。 – 2011-04-06 20:30:57

相關問題