2012-10-06 3 views
0

나는 서버에있는 그림을 검색하려고합니다. 그래서 나는 다음과 같은 방법 일로를 검색하기 위해 노력하고있어 :안드로이드 애플 리케이션에 그림을 표시

 ImageView e = (ImageView) findViewById(R.id.img); 
     e.setImageBitmap(downloadBitmap(img_url(param1.img))); 

하고 XML

 <?xml version="1.0" encoding="utf-8"?> 

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 
    <!-- Name Label --> 
    <ImageView 
     android:id="@+id/img" 
     android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:contentDescription="@string/picture">   
    </ImageView> 

:

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(); 
    } finally { 
     if (client != null) { 
      client.close(); 
     } 
    } 
    return null; 
} 

을 그리고 다른 방법으로 나는 다음 두 줄이

그러나 그림이 표시되지 않고 오류가 발생하지 않습니다. 누구든지 내가 뭘 잘못하고 있다고 말할 수 있습니까?

+0

제공하신 URL이 정확한가요? 그리고 실제로 이미지가 포함되어 있습니까? –

+0

안녕 Vishwa. 예, 저는 URL과 이미지에 대해 100 % 확신합니다. –

답변

0

이미지를 표시하려면 'Universal Image Loader' 라이브러리를 사용하는 것이 좋습니다. 나는이 catch 문에 로그 문을 추가하여 시작할 것

+0

유니버설 이미지 로더는 항상 Android에서 좋은 선택입니다. 위에서 작성한 코드는 큰 이미지 나 여러 이미지를로드 할 때 OutOfMemeory 예외를 제공하기 시작합니다. – Sharj

0

:

} catch (Exception e) { 
    // Could provide a more explicit error message for IOException or IllegalStateException 
    getRequest.abort(); 
} 

그것은 당신이 알고하지 않은 것을 거기에 다른 예외를 던지고 가능성이 있습니다.

관련 문제