2010-03-22 4 views
2

나는 약 45KB to 75KB에 대한 이미지 가져 오기에 대한 모든 표준 네트워크 관련 코드를 사용했지만 모두이 방법이 실패하여 파일의 약 3-5KB 크기 이미지에 적합합니다. 어떻게 내가 사용했던 것들큰 이미지를위한 안드로이드 이미지 게터

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(); 

있습니다 내 네트워크 운영을위한 안드로이드 이미지 뷰에 그들을 표시 45 - 75KB의 이미지를 다운로드 달성하고 내가 사용했던 한 두 번째 옵션은 :

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은 실제로이 문제를 해결할 수있는 것이고 사람을 도와 주신 것에 대해 감사드립니다. –

7

다운로드하는 이미지의 크기는 꽤 관련이 없습니다. BitmapFactory.decodeStream으로 디코딩하는 크기는 이미지 처리에 필요한 메모리입니다. 따라서 reSampling이 유용 할 수 있습니다.

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의 이미지를 다운로드하는 동안 다운로드가 실패하고 결실을 맺는다는 것입니다. 동일한 코드가 문제를 만족시키는 것은 무엇이든 아래에 있습니다. 그게 내 문제입니다. –

+0

감사합니다. 이것은 매우 도움이되었습니다! –

+0

안녕하세요 Robert! "200 * 200 * 2"은 무슨 뜻입니까? –