Stelios Papamichail
Stelios Papamichail

Reputation: 1270

Adding an ImageView to a Constraint Layout causes a crash

I am trying to create a splash screen for my app and it works just fine when I only display some text on the splashScreen's layout file(using a Constraint Layout). When I add an imageView however, the app crashes and I don't get why. The image's file size is only 299kb so it's not a memory issue or something right? Any ideas on why this is happening and how to fix it? Here's my xml file:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal|center_vertical"
    android:orientation="vertical"
    tools:context="com.spdesigns.funfacts.SplashScreen">

<ImageView
    android:id="@+id/logoImageView"
    android:layout_width="377dp"
    android:layout_height="293dp"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@drawable/logo" />

</android.support.constraint.ConstraintLayout>

SplashScreen.java

package com.spdesigns.funfacts;

import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class SplashScreen extends AppCompatActivity {

private static final int Splash_Timer = 4000;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash_screen);
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            Intent mainActivity = new Intent(SplashScreen.this,FunFactsActivity.class);
            startActivity(mainActivity);
            finish();
        }
    },Splash_Timer);
}
}

Logcat Error:

11-15 18:29:19.218 3486-3486/com.spdesigns.funfacts E/AndroidRuntime: FATAL EXCEPTION: main
                                                                  Process: com.spdesigns.funfacts, PID: 3486
                                                                  java.lang.OutOfMemoryError: Failed to allocate a 655089132 byte allocation with 3077156 free bytes and 379MB until OOM
                                                                      at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
                                                                      at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
                                                                      at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:620)
                                                                      at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:455)
                                                                      at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1155)
                                                                      at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:720)
                                                                      at android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:571)
                                                                      at android.content.res.Resources.loadDrawable(Resources.java:854)
                                                                      at android.content.res.TypedArray.getDrawable(TypedArray.java:928)
                                                                      at android.widget.ImageView.<init>(ImageView.java:157)
                                                                      at android.widget.ImageView.<init>(ImageView.java:145)
                                                                      at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:60)
                                                                      at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:56)
                                                                      at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:106)
                                                                      at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1029)
                                                                      at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1087)
                                                                      at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:47)
                                                                      at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:769)
                                                                      at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
                                                                      at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
                                                                      at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
                                                                      at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
                                                                      at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
                                                                      at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
                                                                      at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292)
                                                                      at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
                                                                      at com.spdesigns.funfacts.SplashScreen.onCreate(SplashScreen.java:15)
                                                                      at android.app.Activity.performCreate(Activity.java:6662)
                                                                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                                                                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
                                                                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
                                                                      at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
                                                                      at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                      at android.os.Looper.loop(Looper.java:154)
                                                                      at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

Upvotes: 1

Views: 441

Answers (1)

AndroidRuntimeException
AndroidRuntimeException

Reputation: 2124

The issue is that ImageView uses a GL Texture, which on most devices is limited to 2048x2048. The max texture size varies from device to device, I've seen is as small as ~1500x1500 and as big as 4096x4096.

For that, you have to create an image for each dimension to make sure will work in all devices. Check this thread for more info.

GL Maximum Textures Size Policy

Upvotes: 1

Related Questions