开发者

scaled image not visible in android

开发者 https://www.devze.com 2023-03-14 19:20 出处:网络
I have to make an application in which i am downloading an image from a URL 开发者_如何学Cand i have to show the image to after scaling.

I have to make an application in which i am downloading an image from a URL 开发者_如何学Cand i have to show the image to after scaling.

The default size of the image is 220x200

Scaled size of image is 55x50

Here is my code for doing the above:

XML file layout:

        <ImageView android:id="@+id/productimage"
        android:layout_gravity="center_vertical|left" android:layout_width="55dip"
        android:layout_height="50dip" android:adjustViewBounds="true" />

Here is the code for downloading the image and resizing it:

 ImageView productImage = (ImageView) arg1
                        .findViewById(R.id.productimage);
                if (!headerDetails[1].equals("null")) {
                    Bitmap image = getImage(headerDetails[1]);
                    if (image != null) {
                        try {
                            productImage
                                    .setImageBitmap(Bitmap
                                            .createScaledBitmap(
                                                    image,
                                                    productImage
                                                            .getMeasuredWidth(),
                                                    productImage
                                                            .getMeasuredHeight(),
                                                    false));
                        } catch (Exception e) {

                            e.printStackTrace();
                        }
                    }
                }

Here is the class for downloading the image:

    private Bitmap getImage(String address) {
    try {
        Log.i("getimage", address);
        URL url = new URL(address);

        URLConnection conn = url.openConnection();
        conn.connect();
        InputStream is = conn.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(is);
        Bitmap bm = BitmapFactory.decodeStream(bis);
        // bis.close();
        // is.close();
        return bm;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

No exception is being thrown here, hence the image is being downloaded correctly.

OUTPUT: No image is displayed in the imageview.

what am i doing wrong. The size of the image that i want is 55x50 but the size of the image on the net may vary.

thank you in advance.

EDIT:

the bitmap formed after the following code:

Bitmap bm = BitmapFactory.decodeStream(bis);

does not have a height and a width and it's height and width is set to -1. What could be going wrong here?

The following exception is also being thrown:

06-23 14:24:41.635: WARN/System.err(16618): java.lang.IllegalArgumentException: width and height  must be > 0


I used this code to scale the bitmap:

Bitmap selectedImage = BitmapFactory.decodeFile(selectedImagePath);
image.setImageBitmap(Bitmap.createScaledBitmap(selectedImage,75, 75, true));


Try adding android:scaleType="fix_xy" to the imageview. Another error I found in your code is that you are getting the height and width in a wrong way. Try this:

imageView.getLayoutParams().height;
imageView.getLayoutParams().width;
0

精彩评论

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