2

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.

Phantom
  • 968
  • 3
  • 14
  • 32
  • Not working how? Add android:scaleType="centerInside" and see if they're displayed. – kha Apr 03 '15 at 11:01
  • Not working as nothing is displayed when I try to insert larger images. I will add that line and see what happens. – Phantom Apr 03 '15 at 11:06
  • Nope, still not displaying. – Phantom Apr 03 '15 at 11:08
  • and what happens if you remove the maxHeight and maxWidth attributes from your ImageView? does it work? – kha Apr 03 '15 at 11:15
  • No, it was the initial try. I only added this lines in hoping it will fix the issue, but no success. – Phantom Apr 03 '15 at 11:17
  • are you getting out of memory exceptions or similar or is it just simply blank? This is very strange. Please paste your entire layout that includes the imageview. – kha Apr 03 '15 at 11:19
  • For extra large images 2560x1600 or larger yes. But for my image, which is 1280x720, i don't get the out of memory exception. The entire layout is composed only from the default LinearLayout and the imageView above. – Phantom Apr 03 '15 at 11:29
  • i think you are suffering from memory issues. Try to re-size the image. You can use this code_ [http://stackoverflow.com/questions/8327846/how-to-resize-a-bitmap-eficiently-and-with-out-losing-quality-in-android/8341243#8341243] – Paresh P. Apr 03 '15 at 11:53

6 Answers6

8

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);
Newbie
  • 161
  • 5
  • I have the same problem and tried the above solution but it crashes on the setImageResource(0) with null resource exception. I then commented out this line and tried again. It crashed again with error "java.lang.ClassCastException: android.graphics.drawable.NinePatchDrawable cannot be cast to android.graphics.drawable.BitmapDrawable". – Mobile Developer Jan 24 '16 at 23:16
  • This solution worked for me. I had an ImageView which would display perfectly anywhere in my app apart from inside a Fragment. Annoying that this is necessary, can anyone explain why it's needed? – Jamie Holdstock Nov 06 '16 at 11:22
2

i had same problem sometime ago, my solution was checking the image naming convention example("-" to "_",no space within names, ...) you can check the java naming convention for more information.

rheniytron
  • 21
  • 1
1

Try this: Add these two lines in xml code of your image view:

android:scaleType="fitXY"
android:adjustViewBounds="true"
Rahul Sharma
  • 5,949
  • 5
  • 36
  • 46
0
<ImageView
            android:id="@+id/imageImageView"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:contentDescription="@string/empty"
            android:src="@drawable/test3" />

Try this :)

Anil Meenugu
  • 1,411
  • 1
  • 11
  • 16
0

For me i was getting this error

W/OpenGLRenderer: Bitmap too large to be uploaded into a texture (2250x4386, max=4096x4096)

my image size was actually 750x1462 but because it was sitting in the drawable-mdpi res bucket i think behind the scenes it was being scaled up, so i moved it to xxhdpi and then it loaded the image without a problem.

Fonix
  • 11,447
  • 3
  • 45
  • 74
0

This might have come a little late. I think the filename could also be a problem. I just renamed mine from sebastian-staines-mfzdRsWsiRA-unsplash.jpg to sebastian_staines.jpg in my drawable folder in Android Studio and Boom, The Imageview Displayed it. Hope this helps someone else

Nkem
  • 11
  • 1
  • 4