2011-02-11 83 views
1

ImageViewlist一个s,这从网上下载的图片。
我从服务器下载的图像,并将其显示在ImageView,但.bmp图像不显示ImageView
请帮我解决这个问题。如何设置图像.BMP到ImageView的

回答

0

请检查以下内容代码:

RelativeLayout relLayout = new RelativeLayout(this); 
    URL centreImageURL = new URL(imageUrl); 
        URLConnection conn = centreImageURL.openConnection(); 
        conn.connect(); 
        InputStream is = conn.getInputStream(); 
        BufferedInputStream bis = new BufferedInputStream(is); 
        Bitmap bm = BitmapFactory.decodeStream(is); 
// 110 , 110 are the bitmap width & height 
        Bitmap tempBitmapImg = Bitmap.createScaledBitmap(bm, 110, 110, 
          true); 
        centreImgView.setImageBitmap(tempBitmapImg); 

        RelativeLayout.LayoutParams lp5 = new RelativeLayout.LayoutParams(
          RelativeLayout.LayoutParams.WRAP_CONTENT, 
          RelativeLayout.LayoutParams.WRAP_CONTENT); 
        lp5.setMargins((screenWidth/2) - 50, 
          (screenHeight/2) - 105, 0, 0); 

        relLayout.addView(centreImgView, lp5); 
setContentView(relLayout); 

请回复任何澄清

0

这是您只需传递需要下载的图像网址的方法。

在完成此方法将返回绘制对象,你可以直接设置为一个ImageView的

Drawable drawable_from_url(String url, String src_name) throws java.net.MalformedURLException, java.io.IOException 
{ 
    return Drawable.createFromStream(((java.io.InputStream)new java.net.URL(url).getContent()), src_name); 
} 

使用它,如下

ImageView mImage = (ImageView)findViewById(R.id.MyImageView); 
mImage.setImageDrawable(drawable_from_url("http://your/image/url/here", "src")); 

希望它能帮助:)

+0

此外,如果你正在实现这个ListView与图像列表需要从网络中获取,然后需要显示在列表中。使用线程来下载图像,然后使用Handler类在List中实现Visual Changes,然后确保一次获取的图像不应该再次获取。并且你的列表的getView方法需要很少,否则滚动将会非常缓慢和不平稳 – Javanator 2011-02-11 12:42:52

相关问题