39

Can anyone tell me why the following code is not working? The Image does not appear at all.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"

   <ImageView
          android:src="@drawable/splashscreen"
          android:scaleType="center"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" />

</RelativeLayout>
comead
  • 577
  • 2
  • 13
  • 29

32 Answers32

38

In ImageView there is two options src and srcCompact. Try both of them to find out which is working.

android:src="@drawable/slide_view_4"

and

app:srcCompat="@drawable/slide_view_4"
Mostafa Soliman
  • 725
  • 6
  • 20
34

In my case, all my fragment's imageViews are not showing when I run my application. The problem is, I extend my "Activity" class from "FragmentActivity". so I just change it "FragmentActivity" to "AppCompatActivity"

maybe this will help someone.

bst91
  • 349
  • 3
  • 7
  • 3
    I had this exact situation. Changing my `Activity` to `AppCompatActivity` instead of `FragmentActivity` worked perfectly. Also didn't effect functionality. – WeskerTyrant Jun 07 '17 at 15:43
  • wow exactly, I have recently changed `AppCompatActivity` to `FragmentActivity` for some testing then I forgot to change it back. Now after changing it back to `AppCompatActivity` icons are showing. Do you know reason behind this? – Muhammad Saqib Aug 14 '20 at 12:23
10

Where did you put the source image file? Is that under the Drawable folder? If so, then create mdpi, hdpi, xhdpi, xxhdpi image source for the image, and put them in mdpi, hdpi, xhdpi, xxhdpi folders.

Kai Wang
  • 3,303
  • 1
  • 31
  • 27
8

This is the solution that worked for me after checking the layout xml, theme etc. Just changing the "extends Activity" from your java class to "extends AppCompatActivity".

Should some of the previous suggested solutions worked for you, others having the same problem with ImageView image not showing might try this as well.

Clark Kent
  • 81
  • 1
  • 2
7

I had the same issue and I fixed it by extending AppCompatActivity and renaming my image to correct standard.
My image name (previously): splash-1 (didn't worked).
And I tried renaming image name to splash1.
After that everything worked. note: the image file might also be responsible for this problem.

Jasbin Karki
  • 574
  • 7
  • 15
5

Try to use app:srcCompat="@drawable/modified" instead of tools:srcCompat="@drawable/modified"in XML files.

Bram
  • 2,515
  • 6
  • 36
  • 58
Hammad Hassan
  • 51
  • 1
  • 1
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/29939920) – nnesterov Sep 28 '21 at 15:00
4

Try android:background="@drawable/your_image" instead of app:srcCompat="@drawable/your_image"

SardorbekR
  • 1,388
  • 3
  • 18
  • 33
3

Check for the following:

  1. Are you invoking setContentView (for your splash screen) on the UI thread
  2. Are there other activities (or views) that you're displaying too quickly after your splash screen (i.e. nullifying the splash screen setContentView)?
  3. Trying using match_parent instead of wrap_content on the ImageView layout_height and layout_width.
Avadhani Y
  • 7,566
  • 19
  • 63
  • 90
kenj
  • 130
  • 4
2

Android ImageView Is Not Displaying Image?

none of these solutions are not worked for me. what i did was i added these code lines in my manifest file

android:hardwareAccelerated="false"

android:largeHeap="true"

if these codes line didn't solve your problem try to set your image view height and width. not using wrap content or match parent. if your size didn't worked. try to reduce the number and retry. i think these solutions are helpful.

Community
  • 1
  • 1
Malith Ileperuma
  • 926
  • 11
  • 27
2

Not related to this question but still sharing an experience, sometimes we set the source to the tools and then we expect a result (happened to me twice and took 15 minutes each time to resolve).

tools:srcCompat
//instead of 
android:srcCompat

Image source for tools is visible in android studio only.

Vijay
  • 1,163
  • 8
  • 22
1

In my case, the image wasn't visible while choosing the image from the list of images.

Rebuilding the project fixed it for me.

Build -> Rebuild Project

Rahul Makhija
  • 381
  • 4
  • 7
1

I had this same problem. I really cannot believe how simple it was. Though this may not be the issue for you, this fixed it for me. My image file was named ss-logo.png. Android Studio does not allow dashes in image files. Once I changed it to ss_logo.png, it worked perfectly. Only use letters, numbers, and underscores in your image file name. Hope this helps.

L.C.
  • 29
  • 1
1

Please check your image color...If you are using the white background and if you use the white color image then it will not be displayed...Try changing your background container for the image to some other color or change your image color. I really faced this situation and wasted lot of time...I hope this will help someone...

BharathRao
  • 1,846
  • 1
  • 18
  • 28
1

Try to check the name of your image eg:"img-icon-butterfly.png" these type of names should be renamed and "-" should be removed so instead use names like "imgbutterfly.png"

Mayank
  • 11
  • 4
1

I was facing the exact same problem. I tried rebuild but it did not help. Finally I resized the image from its original size of 2400x2400 to 1200x1200 using an online PNG resizer and then compressed it from 590KB to 150KB file size using an online compressor.

double-beep
  • 5,031
  • 17
  • 33
  • 41
Rohit Pal
  • 15
  • 6
  • No it is not ...because that answer only mentions to reduce image size which i tried but it did not work...then i combined it with resizing the image which turned out to be the formula :) – Rohit Pal Jul 14 '19 at 11:07
1

Try Changing from ImageView to androidx.appcompat.widget.AppCompatImageView in your layout file

Mainong
  • 43
  • 4
1

100% working code . same problem show in my code . but i am using AppCompatImageView after then show my image

<androidx.appcompat.widget.AppCompatImageView
                android:layout_width="50dp"
                android:layout_height="50dp"
                app:srcCompat="@drawable/ic_calendar"
                />
sarjeet singh
  • 461
  • 4
  • 10
1

What format is your drawable file in? Android prefers using .PNG mostly.

Lucifer
  • 29,392
  • 25
  • 90
  • 143
Kurty
  • 475
  • 2
  • 16
  • 1
    what purpose you want to use image?do u want to make splash screen or other???nd mostly the image is in png format.try and check – Google Mar 21 '12 at 04:20
  • 1
    the image is in .PNG format. and yea, it's a splash screen that I just want to display this image in the center of a black screen (as I check for updates from the server, etc. ) – comead Mar 21 '12 at 11:41
  • 1
    Is the image to big? Or does it need to be implemented onto your java file? And if you want to center it, you may want to use Gravity to do that. – Kurty Mar 21 '12 at 17:23
1

A null bitmap = unusable image.

This is the basis for how an image is actually used. When an image is used in android and it is assigned it actually converts the image into a bitmap, and depending on certain parameters being size and various other factors, it can fail, which would result in your image being converted and being returned as null due to some failure that occurred.

JoxTraex
  • 13,423
  • 6
  • 32
  • 45
0

Don't create drawable folder yourself. Recreate it with Image Asset option in android studio.

In my case, the problem was drawable folder itself. i had created drawable folder myself. it's not correct. in the android studio we should create drawable folder with Image Asset option. i didn't get the reason but now the problem is sloved.

Ayub
  • 2,345
  • 27
  • 29
0

try making the image size a little less, I guess the android studio doesn't support the image with higher pixels .Anyway it solved my problem

rubal islam
  • 339
  • 3
  • 5
0

I copied/pasted a PNG image into the Drawable folder and set all the info and params in XML file

---> image showed in the Preview but not on actually device/emulator.

So--> In Java Activity, I recalled it programmatically:

  ImageView imview = (ImageView) findViewById(R.id.IdFromXML);
    imview.setImageResource(0);
Drawable draw = getResources().getDrawable(R.drawable.NameOfTheFileInFolder);
draw = (draw);
imview.setImageDrawable(draw);

Got it done!

Convert Image to PNG if necessary. Plenty free converters online.

Tomkat
  • 3
  • 3
0

Image file name matters as well. Make sure when you name your image, there are no spaces. Use "_" instead or something

Mohammad
  • 95
  • 2
  • 9
0

I was facing the same issue. After a bit of work I found out that if I placed the image in the drawable(anydpi), which was being rendered in the design preview but not on the real device it was a problem. So, I placed the image inside simple drawable folder rather than drawable(anydpi). That fixed my issue.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
0

If you are new to Android Development like I am, READ THESE DOCS:

Android Official Documentation

Medium Article from a LinkedIn Android Dev

I've done iOS development for several years now and decided to branch out into Android development. I found that most of the time it is entirely to do with screen resolution (also mipmap is NOT for drawable images as mentioned before). None of these answers fixed my issues, I had to setup the standard resolution directories of drawable-hdpi, drawable-mdpi, drawable-xhdpi, drawable-xxhdpi, and drawable-xxxhdpi. These are all of the directories your emulator or devices will look at when searching for the correct resolution for the device setup. If you put your image in drawable alone, this could cause these folders to be overlooked by the device and that's when you get a high-resolution image being non-visible or crashing the app from memory errors. Be sure to check the dimensions of your image and make sure that you import it under the correct drawable directories to ensure the device scales the image accordingly.

My explanation is a small overview, but I do highly recommend reading the articles.

0

Look in your logcat output as well. You may see an image similar to this one:

OpenGLRenderer 0x0000644a:0 Bitmap too large to be uploaded into a texture (4203x2277, max=4096x4096)

If this is the case, decrease the image size.

Nathan F.
  • 3,250
  • 3
  • 35
  • 69
0

Try adding this line in ImageView XML file

android:src="@drawable/splashscreen"
tools:srcCompat="@drawable/splashscreen"
sahil
  • 21
  • 4
0

My issue was that the layout alpha was set to 0 which is what went wrong. Sometimes alpha comes by itself. Anyway 0 alpha is the default value even if not mentioned. So try to remove alpha as 0. 0 alpha is = invisible!!

0

in my case replacing tools:srcCompat with android:srcCompat in the xml tag of the image view solved the problem

kamal douma
  • 396
  • 1
  • 7
  • 13
-1

Many of the image related issues are solved by using the third party libraries like Picasso by Jake Wharton. I had unexplained issues with image loading and some times even getting out of memory error. That was especially cased by imaged edited (using Gimp for Mac). Picasso does the hard work of handling the memory and necessary caching etc.

http://square.github.io/picasso/

sivag1
  • 4,734
  • 3
  • 32
  • 35
  • The point of the question is that the image is not being displayed. Picasso's purpose is completely different – Ashok Koyi Nov 24 '15 at 20:32
-1

if your Image is not displaying in your code because of some layout errors you can usetools:ignore="contentDescription"

usetools:ignore="contentDescription"

use this line in code.

-1
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:src="@drawable/ic_launcher" />

</LinearLayout>
Android
  • 1,417
  • 9
  • 11