2013-01-03 232 views
0

我想在imageview上設置圖像,但圖像不顯示。圖像無法顯示圖像視圖

我正在閱讀json數據的圖像url,然後試圖將其設置在ImageView上,但我的圖像不可見。沒有發生任何異常。
這裏是我的代碼

HotelList.class

static final String TAG_DISHIMAGEURL = "dishimageurl"; 
...... 
String imageUrl = dishResult.getString(TAG_DISHIMAGEURL); 
map.put(TAG_DISHIMAGEURL, imageUrl); 
..... 
dishimageurl1 = hm.get(TAG_DISHIMAGEURL).toString(); 
intent.putExtra("background", dishimageurl1); 

HotelDetails.class

...... 
String dishimageurl = bundle.getString("background"); 
Bitmap bimage= getBitmapFromURL(dishimageurl); 
    imageView.setImageBitmap(bimage); 
.... 
public Bitmap getBitmapFromURL(String src) { 
try { 
    URL url = new URL(src); 
    HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
    connection.setDoInput(true); 
    Toast.makeText(this, "showing image", Toast.LENGTH_LONG).show(); 
    connection.connect(); 
    InputStream input = connection.getInputStream(); 
    Bitmap myBitmap = BitmapFactory.decodeStream(input); 
    return myBitmap; 
} catch (IOException e) { 
    Toast.makeText(this, "showing exception", Toast.LENGTH_LONG).show(); 
    return null; 
} 

}

我不明白這個代碼發生什麼。沒有任何例外,但我的圖像不可見。
請給我任何參考。

+0

也許你的問題與佈局問題有關? – Blackbelt

回答

1

請使用下面的代碼從url獲取圖片並顯示到imageview中。

public class image extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     Bitmap bitmap = DownloadImage("http://www.gophoto.it/view.php?i=http://1.bp.blogspot.com/-2LTvCCufBKc/T3L3KgcTj2I/AAAAAAAABbQ/Ki60e1LU9sE/s1600/Sachin%2BTendulkar.png"); 

     RelativeLayout mRlayout1 = (RelativeLayout) findViewById(R.id.mRlayout1); 
     Drawable d=new BitmapDrawable(bitmap); 
     mRlayoutLogin.setBackgroundDrawable(d); 
    } 

    private InputStream OpenHttpConnection(String urlString) throws IOException { 
     InputStream in = null; 
     int response = -1; 

     URL url = new URL(urlString); 
     URLConnection conn = url.openConnection(); 

     if (!(conn instanceof HttpURLConnection)) 
      throw new IOException("Not an HTTP connection"); 

     try { 
      HttpURLConnection httpConn = (HttpURLConnection) conn; 
      httpConn.setAllowUserInteraction(false); 
      httpConn.setInstanceFollowRedirects(true); 
      httpConn.setRequestMethod("GET"); 
      httpConn.connect(); 
      response = httpConn.getResponseCode(); 
      if (response == HttpURLConnection.HTTP_OK) { 
       in = httpConn.getInputStream(); 
      } 
     } catch (Exception ex) { 
      throw new IOException("Error connecting"); 
     } 
     return in; 
    } 

    private Bitmap DownloadImage(String URL) { 
     Bitmap bitmap = null; 
     InputStream in = null; 
     try { 
      in = OpenHttpConnection(URL); 
      bitmap = BitmapFactory.decodeStream(in); 
      in.close(); 
     } catch (IOException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 
     return bitmap; 
    } 
} 
0

您可以通過此代碼查看圖像。

try { 
    bitmap = BitmapFactory.decodeStream((InputStream)new URL(url).getContent()); 
} catch (MalformedURLException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 
0

看來你是下載從UI線程的圖像。這將阻止UI線程,並會給你沒有響應錯誤。作爲一種簡單的方法,你可以使用圖書館像通用圖像裝載機

Universal Image Loader - GitHub

這將管理映像加載爲你和避免這樣不正確的網址,內存不足的錯誤的問題。