Keval
Keval

Reputation: 1602

ImageView problems, image not displaying or throwing error

I am currently trying to place a background image on the login screen of my app. I can't get the image to show up.

1st method tried:

<ImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:srcCompat="@drawable/login_bg"
    android:scaleType="centerCrop"
    android:contentDescription="@string/description_bg" />

This opens the app but just doesn't show the image. (It shows a white background instead).

2nd method tried:

<ImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/login_bg"
    android:scaleType="centerCrop"
    android:contentDescription="@string/description_bg" />

Which returns the error:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: package, PID: 30016
              java.lang.RuntimeException: Unable to start activity ComponentInfo{package/package.LoginActivity}: android.view.InflateException: Binary XML file line #9: Binary XML file line #9: Error inflating class android.widget.ImageView

Full stack trace here.

I am extending Activity in my LoginActivity.

EDIT

I just tried another image and it worked fine. Why is this particular image not working?

My folder structure:

My folder structure

Upvotes: 0

Views: 117

Answers (1)

karthik
karthik

Reputation: 524

I think there is no problem with your XML :

android:src="@drawable/login_bg"

the problem may be with your image login_bg

try a different image in this place login_bg, if it works then your image may be the problem, so try to use a different image.

android:src="@drawable/new_image"

updated answer

so, after checking your edited question :

i think you are using a high-resolution image into your app, it was having an OutOfMemoryError error in your log file.

so you should define android:largeHeap and android:hardwareAccelerated in your manifest like this:

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:hardwareAccelerated="false"
android:theme="@style/AppTheme"
....
....

hope my answer helps you.

Upvotes: 2

Related Questions