5

I am trying to perform an animation on a button click.. the steps are as follows on button click..

  1. the button image changes
  2. an animation plays
  3. next layout is displayed..

But am getting an out of memory exception..

When the animation file was not added to the project there was no error. But since the addition of the anime there is the problem.

I am using 3 class files here(home_screen, button_anime and home)

home_screen.java receives the button click information, changes the button image and transfers to button_anime class the animation file is initiated in the button_anime.java and after anime plays next layout is displayed from home.java

The log cat is as follows..

 E/AndroidRuntime(1255): java.lang.OutOfMemoryError
 E/AndroidRuntime(1255):    at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
 E/AndroidRuntime(1255):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:587)
 E/AndroidRuntime(1255):    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:422)
 E/AndroidRuntime(1255):    at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:840)
 E/AndroidRuntime(1255):    at android.content.res.Resources.loadDrawable(Resources.java:2110)
 E/AndroidRuntime(1255):    at android.content.res.Resources.getDrawable(Resources.java:700)
 E/AndroidRuntime(1255):    at android.graphics.drawable.AnimationDrawable.inflate(AnimationDrawable.java:282)
 E/AndroidRuntime(1255):    at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:937)
 E/AndroidRuntime(1255):    at android.graphics.drawable.Drawable.createFromXml(Drawable.java:877)
 E/AndroidRuntime(1255):    at android.content.res.Resources.loadDrawable(Resources.java:2092)
 E/AndroidRuntime(1255):    at android.content.res.Resources.getDrawable(Resources.java:700)
 E/AndroidRuntime(1255):    at android.view.View.setBackgroundResource(View.java:15303)
 E/AndroidRuntime(1255):    at com.quinoid.thomasinternational.Button_Anime.onCreate(Button_Anime.java:19)
 E/AndroidRuntime(1255):    at android.app.Activity.performCreate(Activity.java:5231)
 E/AndroidRuntime(1255):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
 E/AndroidRuntime(1255):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
 E/AndroidRuntime(1255):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
 E/AndroidRuntime(1255):    at android.app.ActivityThread.access$800(ActivityThread.java:135)
 E/AndroidRuntime(1255):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
 E/AndroidRuntime(1255):    at android.os.Handler.dispatchMessage(Handler.java:102)
 E/AndroidRuntime(1255):    at android.os.Looper.loop(Looper.java:136)
 E/AndroidRuntime(1255):    at android.app.ActivityThread.main(ActivityThread.java:5017)
 E/AndroidRuntime(1255):    at java.lang.reflect.Method.invokeNative(Native Method)
 E/AndroidRuntime(1255):    at java.lang.reflect.Method.invoke(Method.java:515)
 E/AndroidRuntime(1255):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
 E/AndroidRuntime(1255):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
 E/AndroidRuntime(1255):    at dalvik.system.NativeStart.main(Native Method)

My home_screen.java

home.setOnClickListener(new OnClickListener() { <-- error happens somewhere here

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Bitmap b = BitmapFactory.decodeResource(getResources(),R.drawable.home1);
        Drawable d = new BitmapDrawable(getResources(),b); 
        home.setBackgroundDrawable(d); <-- this dose not work..
        Intent myIntent = new Intent(Home_Screen.this, Button_Anime.class);
        startActivity(myIntent);
    }
});
9
  • You're out of memory. It may be the bitmap is too big, it may be that you have a leak elsewhere. You need to look at your memory use in a heap profiler to tell what's going on. Commented Jun 10, 2014 at 6:29
  • all my images are btw 30kb and 50kb.. so how do i check my heap rofiler?? @GabeSechan Commented Jun 10, 2014 at 6:31
  • THere's one built into eclipse. You should look for runaway numbers of some class or large allocations that don't make sense. Commented Jun 10, 2014 at 6:32
  • use android:largeheap="true" in manifest file and use the resize images before your took in to your app.this out of memory exception cause for your app takes run at large heap. Commented Jun 10, 2014 at 6:35
  • done both.. no use.. @prakash Commented Jun 10, 2014 at 6:36

2 Answers 2

5

_img is your imageview. you have to decode your image and set its size like below i am doing.

 File imgFile = new File(_path); // path of your file
    if (imgFile.exists()) {
        FileInputStream fis = new FileInputStream(imgFile);
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 8;
        options.inPurgeable = true;
        options.inScaled = true;
        Bitmap bm = BitmapFactory.decodeStream(fis, null,options);
       _img.setImageBitmap(bm);
Sign up to request clarification or add additional context in comments.

2 Comments

Voted up. This seems to revolve my issue, but I think there is still a memory leak
Very goood. keep it up
0

You should use decodeResource(Resources res, int id, BitmapFactory.Options opts), and specify the inSampleSize -

If set to a value > 1, requests the decoder to subsample the original image, returning a smaller image to save memory.

2 Comments

where should i use this?? can u provide an example? also the SampleSize value is it size in kb?? or dimentions?? @yushulx
Bitmap b = BitmapFactory.decodeResource(getResources(),R.drawable.home1, opts);For example, inSampleSize == 4 returns an image that is 1/4 the width/height of the original, and 1/16 the number of pixels.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.