开发者

How to set .bmp image to ImageView

I have a list of ImageViews, which download the images from the net.

I downloade开发者_开发技巧d the images from server and it displays in ImageView, but .bmp images don't show in ImageView.

Please help me to solve this problem.


Kindly check the following code:

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);

Kindly revert for any clarifications


This is the method you just need to pass the url of your image that you need to download.

This method on completion will return Drawable that you can directly set to a 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);
}   

Use it as below

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

Hope it helps :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜