{"id":1427,"date":"2022-08-30T15:16:26","date_gmt":"2022-08-30T15:16:26","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/16\/android-imageview-setimageresourceanimationdrawable-causes-memory-error-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:16:26","modified_gmt":"2022-08-30T15:16:26","slug":"android-imageview-setimageresourceanimationdrawable-causes-memory-error-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/android-imageview-setimageresourceanimationdrawable-causes-memory-error-collection-of-common-programming-errors\/","title":{"rendered":"Android: imageView.setImageResource(AnimationDrawable) causes memory error-Collection of common programming errors"},"content":{"rendered":"<p>Here is what I am doing<\/p>\n<pre><code>ImageView image2 = (ImageView) findViewById(R.id.imageView3);\nimage2.getDrawable().setCallback(null);\nimage2.setImageResource(R.drawable.fire);\n<\/code><\/pre>\n<p>where fire is an xml file proposing an animation drawable<\/p>\n<pre><code>\n\n\n\n\n\n\n...\n\n<\/code><\/pre>\n<p>I have this error<\/p>\n<pre><code>02-12 11:37:20.919: E\/dalvikvm-heap(3193): 1535664-byte external allocation too large for this process.\n02-12 11:37:20.959: E\/GraphicsJNI(3193): VM won't let us allocate 1535664 bytes\n02-12 11:37:20.959: W\/dalvikvm(3193): threadid=1: thread exiting with uncaught exception (group=0x40018560)\n02-12 11:37:20.979: E\/AndroidRuntime(3193): FATAL EXCEPTION: main\n02-12 11:37:20.979: E\/AndroidRuntime(3193): java.lang.OutOfMemoryError: bitmap size exceeds VM budget\n02-12 11:37:20.979: E\/AndroidRuntime(3193):     at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)\n02-12 11:37:20.979: E\/AndroidRuntime(3193):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:460)\n02-12 11:37:20.979: E\/AndroidRuntime(3193):     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336)\n02-12 11:37:20.979: E\/AndroidRuntime(3193):     at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)\n02-12 11:37:20.979: E\/AndroidRuntime(3193):     at android.content.res.Resources.loadDrawable(Resources.java:1709)\n02-12 11:37:20.979: E\/AndroidRuntime(3193):     at android.content.res.Resources.getDrawable(Resources.java:581)\n02-12 11:37:20.979: E\/AndroidRuntime(3193):     at android.graphics.drawable.AnimationDrawable.inflate(AnimationDrawable.java:267)\n02-12 11:37:20.979: E\/AndroidRuntime(3193):     at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:787)\n02-12 11:37:20.979: E\/AndroidRuntime(3193):     at android.graphics.drawable.Drawable.createFromXml(Drawable.java:728)\n02-12 11:37:20.979: E\/AndroidRuntime(3193):     at android.content.res.Resources.loadDrawable(Resources.java:1694)\n02-12 11:37:20.979: E\/AndroidRuntime(3193):     at android.content.res.Resources.getDrawable(Resources.java:581)\n02-12 11:37:20.979: E\/AndroidRuntime(3193):     at android.widget.ImageView.resolveUri(ImageView.java:501)\n02-12 11:37:20.979: E\/AndroidRuntime(3193):     at android.widget.ImageView.setImageResource(ImageView.java:280)\n02-12 11:37:20.979: E\/AndroidRuntime(3193):     at com.example.kersplatt.GameActivity.onCreate(GameActivity.java:71)\n02-12 11:37:20.979: E\/AndroidRuntime(3193):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)\n02-12 11:37:20.979: E\/AndroidRuntime(3193):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)\n02-12 11:37:20.979: E\/AndroidRuntime(3193):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)\n02-12 11:37:20.979: E\/AndroidRuntime(3193):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)\n02-12 11:37:20.979: E\/AndroidRuntime(3193):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)\n02-12 11:37:20.979: E\/AndroidRuntime(3193):     at android.os.Handler.dispatchMessage(Handler.java:99)\n02-12 11:37:20.979: E\/AndroidRuntime(3193):     at android.os.Looper.loop(Looper.java:130)\n02-12 11:37:20.979: E\/AndroidRuntime(3193):     at android.app.ActivityThread.main(ActivityThread.java:3683)\n02-12 11:37:20.979: E\/AndroidRuntime(3193):     at java.lang.reflect.Method.invokeNative(Native Method)\n02-12 11:37:20.979: E\/AndroidRuntime(3193):     at java.lang.reflect.Method.invoke(Method.java:507)\n02-12 11:37:20.979: E\/AndroidRuntime(3193):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:875)\n02-12 11:37:20.979: E\/AndroidRuntime(3193):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:633)\n02-12 11:37:20.979: E\/AndroidRuntime(3193):     at dalvik.system.NativeStart.main(Native Method)\n<\/code><\/pre>\n<p>I read so many threads about memory leaks, memory errors (for instance java.lang.OutOfMemoryError: bitmap size exceeds VM budget &#8211; Android) but I didn&#8217;t succeed in solving my problem<\/p>\n<p>When I remplace <code>image2.setImageResource(R.drawable.fire);<\/code> by <code>image2.setImageResource(R.drawable.fire1);<\/code> where fire1 is a simple png, everything works fine What could I do to solve my problem<\/p>\n<p id=\"rop\"><small>Originally posted 2013-11-16 20:50:36. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>Here is what I am doing ImageView image2 = (ImageView) findViewById(R.id.imageView3); image2.getDrawable().setCallback(null); image2.setImageResource(R.drawable.fire); where fire is an xml file proposing an animation drawable &#8230; I have this error 02-12 11:37:20.919: E\/dalvikvm-heap(3193): 1535664-byte external allocation too large for this process. 02-12 11:37:20.959: E\/GraphicsJNI(3193): VM won&#8217;t let us allocate 1535664 bytes 02-12 11:37:20.959: W\/dalvikvm(3193): threadid=1: thread exiting [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1427","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1427","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1427"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1427\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1427"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1427"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1427"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}