在Android应用程序上显示图片

在Android应用程序上显示图片,android,Android,我正在尝试检索服务器上的图片。因此,我尝试使用以下方法检索它: static Bitmap downloadBitmap(String url) { final AndroidHttpClient client = AndroidHttpClient.newInstance("Android"); final HttpGet getRequest = new HttpGet(url); try { HttpResponse response = cl

我正在尝试检索服务器上的图片。因此,我尝试使用以下方法检索它:

  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;
}
在另一种方法中,我有以下两行:

      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>

但是图片没有显示,我没有收到任何错误。
有人能告诉我我做错了什么吗?

我建议使用库来显示图像

首先,我将向这个catch语句添加一个log语句:

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

它可能会抛出另一个你不知道的异常。

你确定你提供的url是正确的吗?它确实包含一个图像?嗨,维什瓦。是的,我对url和图像有100%的把握。在Android上,通用图像加载器总是一个不错的选择。当加载大图像或多个图像时,上面编写的代码将开始发出OutOfMemory异常。