2010-03-22 54 views
2

我已經使用所有標準網絡相關代碼獲取圖像約45KB to 75KB,但所有這些方法都失敗,這些方法適用於大小爲3-5KB大小的圖像。我怎樣才能實現下載的45 - 75KB圖像對Android中爲我的網絡運營的ImageView的顯示他們我已經使用過的東西是Android Image Getter for Larger Images

final URL url = new URL(urlString); 

final URLConnection conn = url.openConnection(); 
HttpURLConnection httpConn = (HttpURLConnection) conn; 

httpConn.setAllowUserInteraction(true); 

httpConn.setInstanceFollowRedirects(true); 

httpConn.setRequestMethod("GET"); 

httpConn.connect(); 

而且,我已經用了第二個選項是::

DefaultHttpClient httpClient = new DefaultHttpClient(); 

HttpGet getRequest = new HttpGet(urlString); 

HttpResponse response = httpClient.execute(getRequest); 

爲什麼此代碼可用於較小尺寸的圖像,而不適用於較大尺寸的圖像。 ?

回答

2

很高興看到用於將響應解碼爲位圖的代碼類型。無論如何,嘗試使用這樣的BufferedInputStream:

public Bitmap getRemoteImage(final URL aURL) { 
    try { 
    final URLConnection conn = aURL.openConnection(); 
    conn.connect(); 
    final BufferedInputStream bis = new BufferedInputStream(conn.getInputStream()); 
    final Bitmap bm = BitmapFactory.decodeStream(bis); 
    return bm; 
    } catch (IOException e) { 
    Log.d("DEBUGTAG", "Oh noooz an error..."); 
    } 
    return null; 
} 
+0

雅PHP_Jedi的BufferedInputStream是真的是我想的,這是真的東西會解決問題,幫助人是感謝。 – 2010-03-28 07:58:30

7

您下載的圖像的大小是非常不相關的。但是,使用BitmapFactory.decodeStream解碼的大小是您需要處理圖像的內存。 因此重新採樣可能是有用的。

Options options = new BitmapFactory.Options(); 
    options.inJustDecodeBounds = true; 

    BitmapFactory.decodeStream(is, null, options); 

    Boolean scaleByHeight = Math.abs(options.outHeight - TARGET_HEIGHT) >= Math.abs(options.outWidth - TARGET_WIDTH); 

    if(options.outHeight * options.outWidth >= 200*200){ 
    // Load, scaling to smallest power of 2 if dimensions >= desired dimensions 
    double sampleSize = scaleByHeight 
      ? options.outHeight/TARGET_HEIGHT 
      : options.outWidth/TARGET_WIDTH; 
    options.inSampleSize = 
      (int)Math.pow(2d, Math.floor(
      Math.log(sampleSize)/Math.log(2d))); 
    } 

    // Do the actual decoding 
    options.inJustDecodeBounds = false; 

    is.close(); 
    is = getHTTPConnectionInputStream(sUrl); 
    Bitmap img = BitmapFactory.decodeStream(is, null, options); 
    is.close(); 
+0

Robert Foss我在這裏談論的大小不是圖像有限大小,而是服務器上圖像的大小,問題是在下載40K圖像時,下載變得不成功並且毫無結果。低於相同的代碼就可以滿足這個問題。那是我的問題是什麼。 – 2010-03-28 08:02:26

+0

謝謝..這非常有幫助! – 2010-05-17 01:23:38

+0

嗨羅伯特! 「200 * 200 * 2」是什麼意思? – 2010-06-19 17:02:01