1

In my application I have over 20 pictures that are layouts backgrounds. When I added another image, the application has stopped working.

01-20 22:01:51.707: E/AndroidRuntime(15045): FATAL EXCEPTION: main
01-20 22:01:51.707: E/AndroidRuntime(15045): Process: xxxxxx, PID: 15045
01-20 22:01:51.707: E/AndroidRuntime(15045): java.lang.OutOfMemoryError
01-20 22:01:51.707: E/AndroidRuntime(15045):    at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
01-20 22:01:51.707: E/AndroidRuntime(15045):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:677)
01-20 22:01:51.707: E/AndroidRuntime(15045):    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:507)
01-20 22:01:51.707: E/AndroidRuntime(15045):    at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:872)
01-20 22:01:51.707: E/AndroidRuntime(15045):    at android.content.res.Resources.loadDrawable(Resources.java:3056)
01-20 22:01:51.707: E/AndroidRuntime(15045):    at android.content.res.Resources.getDrawable(Resources.java:1613)
01-20 22:01:51.707: E/AndroidRuntime(15045):    at com.android.internal.widget.ActionBarView.setIcon(ActionBarView.java:1016)
01-20 22:01:51.707: E/AndroidRuntime(15045):    at com.android.internal.policy.impl.PhoneWindow.setDefaultIcon(PhoneWindow.java:1543)
01-20 22:01:51.707: E/AndroidRuntime(15045):    at android.app.Activity.initActionBar(Activity.java:1996)
01-20 22:01:51.707: E/AndroidRuntime(15045):    at android.app.Activity.setContentView(Activity.java:2011)
01-20 22:01:51.707: E/AndroidRuntime(15045):    at xxxxxx.onCreate(xxxxx.java:1338)
01-20 22:01:51.707: E/AndroidRuntime(15045):    at android.app.Activity.performCreate(Activity.java:5426)
01-20 22:01:51.707: E/AndroidRuntime(15045):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
01-20 22:01:51.707: E/AndroidRuntime(15045):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
01-20 22:01:51.707: E/AndroidRuntime(15045):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
01-20 22:01:51.707: E/AndroidRuntime(15045):    at android.app.ActivityThread.access$900(ActivityThread.java:161)
01-20 22:01:51.707: E/AndroidRuntime(15045):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
01-20 22:01:51.707: E/AndroidRuntime(15045):    at android.os.Handler.dispatchMessage(Handler.java:102)
01-20 22:01:51.707: E/AndroidRuntime(15045):    at android.os.Looper.loop(Looper.java:157)
01-20 22:01:51.707: E/AndroidRuntime(15045):    at android.app.ActivityThread.main(ActivityThread.java:5356)
01-20 22:01:51.707: E/AndroidRuntime(15045):    at java.lang.reflect.Method.invokeNative(Native Method)
01-20 22:01:51.707: E/AndroidRuntime(15045):    at java.lang.reflect.Method.invoke(Method.java:515)
01-20 22:01:51.707: E/AndroidRuntime(15045):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
01-20 22:01:51.707: E/AndroidRuntime(15045):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
01-20 22:01:51.707: E/AndroidRuntime(15045):    at dalvik.system.NativeStart.main(Native Method)

Images are loaded at the same time in the method onCreate().

main.xml:

<LinearLayout
    android:id="@+id/linearLayout27"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/abc"
    android:orientation="vertical" >

onCreate():

layout26 = (LinearLayout) findViewById(R.id.linearLayout27);
layout26.setOnClickListener(this);

I should add that each image has a size of approximately 400 x 800 pixels.

I know that this problem has already appeared here, but I could not find a solution for my case.

pawel22
  • 23
  • 5

1 Answers1

2

It's a very common issue when working with android Bitmap. I genrally add this

android:largeHeap=“true”

in the manifest file, But of lately i have been using Picasso jar which is very good while handling images. It provides cache by default also. Here's a link to get you started http://square.github.io/picasso/

Aniruddha K.M
  • 7,361
  • 3
  • 43
  • 52
  • Can I load image using Picasso as a background to a layout – pawel22 Jan 21 '15 at 11:18
  • i didnt get what you are trying to ask, are you asking if you can load the images as a async task in the background inside a layout OR something else – Aniruddha K.M Jan 21 '15 at 11:22
  • In the documentation of Picasso library I found this :Picasso.with(context).load(R.drawable.landing_screen).into(imageView1); But I need to load image as a layout background. And it seems that in this library I can only use imageView – pawel22 Jan 21 '15 at 11:32
  • yes it works only with imageview, but you can set your layout background using imageview also refer this http://stackoverflow.com/questions/20928748/using-imageview-as-background-for-an-android-layout – Aniruddha K.M Jan 21 '15 at 11:39