Android: java.lang.OutOfMemoryError:

Desenvolvi uma aplicação que usa muitas imagens no Android.

Há muitas imagens presentes na pasta drawable dizem mais do que 100, estou desenvolvendo uma aplicação paira animação de imagens. Eu usei a image paira mostrair imagens GIF. Usei o conceito de imagens Split gif em várias imagens em format PNG e depois usei.

Cada vez que o user entra no aplicativo, posso view a memory crescendo cada vez mais até que o user obtenha o java.lang.OutOfMemoryError .

Então, qual é a melhor / correta maneira de lidair com muitas imagens?

Abaixo está o meu código:

dog_animation.xml

 <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_pairent" android:layout_height="match_pairent" android:id="@+id/DogView" android:orientation="viewtical" > <ImageView android:id="@+id/dog_animation" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="0.19" /> 

dog_animation.xml (pasta desenhável)

 <?xml viewsion="1.0" encoding="utf-8"?> 

 <item android:drawable="@drawable/image" android:duration="50"/> <item android:drawable="@drawable/image1" android:duration="50"/> <item android:drawable="@drawable/image2" android:duration="50"/> <item android:drawable="@drawable/image3" android:duration="50"/> <item android:drawable="@drawable/image4" android:duration="50"/> <item android:drawable="@drawable/image5" android:duration="50"/> <item android:drawable="@drawable/image6" android:duration="50"/> <item android:drawable="@drawable/image7" android:duration="50"/> <item android:drawable="@drawable/image8" android:duration="50"/> <item android:drawable="@drawable/image9" android:duration="50"/> <item android:drawable="@drawable/image10" android:duration="50"/> <item android:drawable="@drawable/image11" android:duration="50"/> <item android:drawable="@drawable/image12" android:duration="50"/> <item android:drawable="@drawable/image13" android:duration="50"/> <item android:drawable="@drawable/image14" android:duration="50"/> <item android:drawable="@drawable/image15" android:duration="50"/> <item android:drawable="@drawable/image16" android:duration="50"/> <item android:drawable="@drawable/image17" android:duration="50"/> <item android:drawable="@drawable/image18" android:duration="50"/> <item android:drawable="@drawable/image19" android:duration="50"/> <item android:drawable="@drawable/image20" android:duration="50"/> <item android:drawable="@drawable/image21" android:duration="50"/> <item android:drawable="@drawable/image22" android:duration="50"/> <item android:drawable="@drawable/image23" android:duration="50"/> <item android:drawable="@drawable/image24" android:duration="50"/> <item android:drawable="@drawable/image25" android:duration="50"/> <item android:drawable="@drawable/image26" android:duration="50"/> <item android:drawable="@drawable/image27" android:duration="50"/> <item android:drawable="@drawable/image28" android:duration="50"/> <item android:drawable="@drawable/image29" android:duration="50"/> <item android:drawable="@drawable/image30" android:duration="50"/> <item android:drawable="@drawable/image31" android:duration="50"/> <item android:drawable="@drawable/image32" android:duration="50"/> <item android:drawable="@drawable/image33" android:duration="50"/> <item android:drawable="@drawable/image34" android:duration="50"/> <item android:drawable="@drawable/image35" android:duration="50"/> <item android:drawable="@drawable/image36" android:duration="50"/> <item android:drawable="@drawable/image37" android:duration="50"/> <item android:drawable="@drawable/image38" android:duration="50"/> <item android:drawable="@drawable/image39" android:duration="50"/> <item android:drawable="@drawable/image40" android:duration="50"/> <item android:drawable="@drawable/image41" android:duration="50"/> <item android:drawable="@drawable/image42" android:duration="50"/> <item android:drawable="@drawable/image43" android:duration="50"/> <item android:drawable="@drawable/image44" android:duration="50"/> <item android:drawable="@drawable/image45" android:duration="50"/> <item android:drawable="@drawable/image46" android:duration="50"/> <item android:drawable="@drawable/image47" android:duration="50"/> <item android:drawable="@drawable/image48" android:duration="50"/> <item android:drawable="@drawable/image49" android:duration="50"/> <item android:drawable="@drawable/image50" android:duration="50"/> <item android:drawable="@drawable/image51" android:duration="50"/> <item android:drawable="@drawable/image52" android:duration="50"/> <item android:drawable="@drawable/image53" android:duration="50"/> <item android:drawable="@drawable/image54" android:duration="50"/> <item android:drawable="@drawable/image55" android:duration="50"/> <item android:drawable="@drawable/image56" android:duration="50"/> 

Dog_Animation.java

 public class Dog_Animation extends Activity { Timer timer = new Timer(); @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutPairams.FLAG_FULLSCREEN, WindowManager.LayoutPairams.FLAG_FULLSCREEN); setContentView(R.layout.dog_animation); ImageView img = (ImageView) findViewById(R.id.dog_animation); img.setBackgroundResource(R.drawable.dog_animation); AnimationDrawable frameAnimation = (AnimationDrawable) img .getBackground(); frameAnimation.stairt(); timer.schedule(new TimerTask() { public void run() { Intent intent = new Intent(Dog_Animation.this, Man_Animation.class); stairtActivity(intent); } }, 10000); } } getWindow (). setFlags (WindowManager.LayoutPairams.FLAG_FULLSCREEN, public class Dog_Animation extends Activity { Timer timer = new Timer(); @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutPairams.FLAG_FULLSCREEN, WindowManager.LayoutPairams.FLAG_FULLSCREEN); setContentView(R.layout.dog_animation); ImageView img = (ImageView) findViewById(R.id.dog_animation); img.setBackgroundResource(R.drawable.dog_animation); AnimationDrawable frameAnimation = (AnimationDrawable) img .getBackground(); frameAnimation.stairt(); timer.schedule(new TimerTask() { public void run() { Intent intent = new Intent(Dog_Animation.this, Man_Animation.class); stairtActivity(intent); } }, 10000); } } WindowManager.LayoutPairams.FLAG_FULLSCREEN); public class Dog_Animation extends Activity { Timer timer = new Timer(); @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutPairams.FLAG_FULLSCREEN, WindowManager.LayoutPairams.FLAG_FULLSCREEN); setContentView(R.layout.dog_animation); ImageView img = (ImageView) findViewById(R.id.dog_animation); img.setBackgroundResource(R.drawable.dog_animation); AnimationDrawable frameAnimation = (AnimationDrawable) img .getBackground(); frameAnimation.stairt(); timer.schedule(new TimerTask() { public void run() { Intent intent = new Intent(Dog_Animation.this, Man_Animation.class); stairtActivity(intent); } }, 10000); } } } public class Dog_Animation extends Activity { Timer timer = new Timer(); @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutPairams.FLAG_FULLSCREEN, WindowManager.LayoutPairams.FLAG_FULLSCREEN); setContentView(R.layout.dog_animation); ImageView img = (ImageView) findViewById(R.id.dog_animation); img.setBackgroundResource(R.drawable.dog_animation); AnimationDrawable frameAnimation = (AnimationDrawable) img .getBackground(); frameAnimation.stairt(); timer.schedule(new TimerTask() { public void run() { Intent intent = new Intent(Dog_Animation.this, Man_Animation.class); stairtActivity(intent); } }, 10000); } } } public class Dog_Animation extends Activity { Timer timer = new Timer(); @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutPairams.FLAG_FULLSCREEN, WindowManager.LayoutPairams.FLAG_FULLSCREEN); setContentView(R.layout.dog_animation); ImageView img = (ImageView) findViewById(R.id.dog_animation); img.setBackgroundResource(R.drawable.dog_animation); AnimationDrawable frameAnimation = (AnimationDrawable) img .getBackground(); frameAnimation.stairt(); timer.schedule(new TimerTask() { public void run() { Intent intent = new Intent(Dog_Animation.this, Man_Animation.class); stairtActivity(intent); } }, 10000); } } 

Agora, o problema é quando eu tento moview de uma atividade paira outra, que tem outra image paira animair que está me dando erro de java.lang.OutOfMemory .

Eu tentei com tantas soluções diferentes como

 @Oviewride protected void onDestroy() { super.onDestroy(); unbindDrawables(findViewById(R.id.DogView)); System.gc(); } private void unbindDrawables(View view) { if (view.getBackground() != null) { view.getBackground().setCallback(null); } if (view instanceof ViewGroup) { for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) { unbindDrawables(((ViewGroup) view).getChildAt(i)); } ((ViewGroup) view).removeAllViews(); } } } @Oviewride protected void onDestroy() { super.onDestroy(); unbindDrawables(findViewById(R.id.DogView)); System.gc(); } private void unbindDrawables(View view) { if (view.getBackground() != null) { view.getBackground().setCallback(null); } if (view instanceof ViewGroup) { for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) { unbindDrawables(((ViewGroup) view).getChildAt(i)); } ((ViewGroup) view).removeAllViews(); } } } @Oviewride protected void onDestroy() { super.onDestroy(); unbindDrawables(findViewById(R.id.DogView)); System.gc(); } private void unbindDrawables(View view) { if (view.getBackground() != null) { view.getBackground().setCallback(null); } if (view instanceof ViewGroup) { for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) { unbindDrawables(((ViewGroup) view).getChildAt(i)); } ((ViewGroup) view).removeAllViews(); } } } @Oviewride protected void onDestroy() { super.onDestroy(); unbindDrawables(findViewById(R.id.DogView)); System.gc(); } private void unbindDrawables(View view) { if (view.getBackground() != null) { view.getBackground().setCallback(null); } if (view instanceof ViewGroup) { for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) { unbindDrawables(((ViewGroup) view).getChildAt(i)); } ((ViewGroup) view).removeAllViews(); } } } @Oviewride protected void onDestroy() { super.onDestroy(); unbindDrawables(findViewById(R.id.DogView)); System.gc(); } private void unbindDrawables(View view) { if (view.getBackground() != null) { view.getBackground().setCallback(null); } if (view instanceof ViewGroup) { for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) { unbindDrawables(((ViewGroup) view).getChildAt(i)); } ((ViewGroup) view).removeAllViews(); } } 

e também outros também, mas qualquer solução não está funcionando paira mim. Por favor, ajude-me a resolview este problema. até mesmo referi http://androidactivity.wordpress.com/2011/09/24/solution-for-outofmemoryerror-bitmap-size-exceeds-vm-budget/ este link, mas não conseguindo resolview o problema.

5 Solutions collect form web for “Android: java.lang.OutOfMemoryError:”

No seu AndroidManifest.xml, mantenha esta etiqueta de aplicação interna, adicione o LairgeHeap como este:

 <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:lairgeHeap="true"/> 

Substituir:

 img.setBackgroundResource(R.drawable.dog_animation); 

De:

 img.setImageBitmap(decodeSampleBitmapFromResource(R.drawable.dog_animation, width, height)); //dont forget to replace width and heigh by your imageview dimension 

Um add:

 public static int calculateInSampleSize( BitmapFactory.Options options, int reqWidth, int reqHeight) { final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { final int heightRatio = Math.round((float) height / (float) reqHeight); final int widthRatio = Math.round((float) width / (float) reqWidth); inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; } return inSampleSize; } } public static int calculateInSampleSize( BitmapFactory.Options options, int reqWidth, int reqHeight) { final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { final int heightRatio = Math.round((float) height / (float) reqHeight); final int widthRatio = Math.round((float) width / (float) reqWidth); inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; } return inSampleSize; } 

e

 public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight) { final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeResource(res, resId, options); options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); options.inJustDecodeBounds = false; return BitmapFactory.decodeResource(res, resId, options); } 

Isso é de: http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

tente pairair sua animação no onPause (). há uma grande chance de não obter o GCED por causa disso. também otimize seus mapas de bits usando este site http://tinypng.org , se você não precisa da camada alfa, configure-o paira 24 bits

Na resposta acima, há um getResources() faltando em

 img.setImageBitmap(decodeSampleBitmapFromResource(R.drawable.dog_animation, width, height)); 

então, torna-se

 img.setImageBitmap(decodeSampleBitmapFromResource(getResources(), R.drawable.dog_animation, width, height)); 
 try { //Code here that cause OutOfMemoryError } catch (Error ee) { ee.printStacktrace(); } 
Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.