如何将.bmp图像设置为ImageView

时间:2011-02-11 12:33:25

标签: android android-imageview

我有list ImageView个,从网上下载图像 我从服务器下载了图片,它显示在ImageView,但.bmp图像未显示在ImageView
请帮我解决这个问题。

2 个答案:

答案 0 :(得分:0)

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

此方法完成后将返回Drawable,您可以直接将其设置为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"));

希望有所帮助:)

答案 1 :(得分: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);

请回复任何澄清