Search code examples
androidandroid-imageview

ImageView not displaying some images


I'm trying to display an image in Android's ImageView , but for an unknown reason some images are not displayed. The images that are working are relatively small and the ones not working are larger (1280x720) or so. I even tried to limit the image's size, by adding maxWidth or maxHeight in .xml file or even by code, but still no result.

Here's my .xml code for ImageView;

<ImageView
            android:id="@+id/imageImageView"
            android:layout_width="fill_parent"
            android:maxHeight="100dp"
            android:maxWidth="100dp"
            android:layout_height="wrap_content"
            android:contentDescription="@string/empty"
            android:gravity="center"
            android:src="@drawable/test3" />

And for my code:

ImageView imageView = (ImageView) view
                .findViewById(R.id.imageImageView);
        imageView.requestLayout();
        imageView.getLayoutParams().height = 100;
        imageView.getLayoutParams().width = 100;

Any ideas / suggestions ? Thank you.


Solution

  • Try resizing your image like :

    private Drawable resize(Drawable image) {
        Bitmap bitmap = ((BitmapDrawable) image).getBitmap();
        Bitmap bitmapResized = Bitmap.createScaledBitmap(bitmap,
                (int) (bitmap.getWidth() * 0.5), (int) (bitmap.getHeight() * 0.5), false);
        return new BitmapDrawable(getResources(), bitmapResized);
    }
    

    And in your onCreateView() of your fragment:

    ImageView imageView = (ImageView) view
                    .findViewById(R.id.imageImageView);
            imageView.setImageResource(0);
            Drawable draw = getResources().getDrawable(R.drawable.test3);
            draw = resize(draw);
            imageView.setImageDrawable(draw);