-2

My app works finely with 11 image views. But upon adding 1 more image view to it, app crashes. I found some similar answers to this problem in stack overflow itself, but couldn't find my exact solution.

Main Activity:

    package com.trendsetter.vyshnav.eatutor;
    import android.os.Bundle;
    import android.support.v7.app.ActionBarActivity;


    public class BreakfastActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_breakfast);
}

    }

XML file:

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp"
android:fillViewport="true">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/t1"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#006600"
        android:paddingBottom="10dp" />
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="200dp"
        android:scaleType="centerCrop"
        android:src="@drawable/i1" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/d1a"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:paddingTop="10dp" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/d1b"
        android:textAppearance="?android:attr/textAppearanceMedium" />


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/t2"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:paddingTop="10dp"
        android:textColor="#006600"
        android:paddingBottom="10dp" />
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="200dp"
        android:scaleType="centerCrop"
        android:src="@drawable/i2" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/d2"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:paddingTop="10dp" />



    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/t3"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#006600"
        android:paddingBottom="10dp"
        android:paddingTop="10dp" />
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="200dp"
        android:scaleType="centerCrop"
        android:src="@drawable/i3" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/d3a"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:paddingTop="10dp" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/d3b"
        android:textAppearance="?android:attr/textAppearanceMedium" />


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/t4"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#006600"
        android:paddingBottom="10dp"
        android:paddingTop="10dp" />
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="200dp"
        android:scaleType="centerCrop"
        android:src="@drawable/i4" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/d4a"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:paddingTop="10dp" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/d4b"
        android:textAppearance="?android:attr/textAppearanceMedium" />


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/t5"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#006600"
        android:paddingBottom="10dp"
        android:paddingTop="10dp" />
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="200dp"
        android:scaleType="centerCrop"
        android:src="@drawable/i5" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/d5a"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:paddingTop="10dp" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/d5b"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/t6"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#006600"
        android:paddingBottom="10dp"
        android:paddingTop="10dp" />
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="200dp"
        android:scaleType="centerCrop"
        android:src="@drawable/i6" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/d6a"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:paddingTop="10dp" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/d6b"
        android:textAppearance="?android:attr/textAppearanceMedium" />


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/t7"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#006600"
        android:paddingBottom="10dp"
        android:paddingTop="10dp" />
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="200dp"
        android:scaleType="centerCrop"
        android:src="@drawable/i7" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/d7a"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:paddingTop="10dp" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/d7b"
        android:textAppearance="?android:attr/textAppearanceMedium" />


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/t8"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#006600"
        android:paddingBottom="10dp"
        android:paddingTop="10dp" />
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="200dp"
        android:scaleType="centerCrop"
        android:src="@drawable/i8" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/d8a"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:paddingTop="10dp" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/d8b"
        android:textAppearance="?android:attr/textAppearanceMedium" />


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/t9"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#006600"
        android:paddingBottom="10dp"
        android:paddingTop="10dp" />
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="200dp"
        android:scaleType="centerCrop"
        android:src="@drawable/i9" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/d9a"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:paddingTop="10dp" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/d9b"
        android:textAppearance="?android:attr/textAppearanceMedium" />


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/t10"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#006600"
        android:paddingBottom="10dp"
        android:paddingTop="10dp" />
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="200dp"
        android:scaleType="centerCrop"
        android:src="@drawable/i10" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/d10a"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:paddingTop="10dp" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/d10b"
        android:textAppearance="?android:attr/textAppearanceMedium" />


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/t11"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#006600"
        android:paddingBottom="10dp"
        android:paddingTop="10dp" />
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="200dp"
        android:scaleType="centerCrop"
        android:src="@drawable/i11" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/d11a"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:paddingTop="10dp" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/d11b"
        android:textAppearance="?android:attr/textAppearanceMedium" />


</LinearLayout>
    </ScrollView>

Logcat:

    Caused by: java.lang.OutOfMemoryError: Failed to allocate a 9815052 byte allocation with 4194208 free bytes and 5MB until OOM
vvv
  • 153
  • 1
  • 7

3 Answers3

0

Adding AndroidManifest.xml file and solve issue:

<application
             ....
             ....
             android:largeHeap="true"

 </application>
Prakash Gajera
  • 563
  • 1
  • 6
  • 18
0

Your Scrollview seems to be a collection of multiple sets of 3 TextViews plus an ImageView.

I suggest defining a custom Object with 3 String values and an int value that will hold the R.drawable integer value for an image.

Then, you can define a custom XML row a single object. And "bind" each object to the Views via a ListView + ArrayAdapter or RecyclerView.

In order to fix the out of memory error, you should use an image loading library like Picasso or Glide to load the integer resource id mentioned earlier into the ImageView rather than load the full, raw image into memory.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0

It is happening because your image size is larger than 2Mb so try to compress the image in your baseAdapter: yo can follow this link to compress the image : How to compress image size?

Community
  • 1
  • 1
Amit Ranjan
  • 567
  • 2
  • 11