I have asked for help about OutOfMemoryError problem in my android app before. Since I have not had a solution yet i need to ask you again to advice me a solution. I tested my app in several real android devices. But in Samsung Galaxy S3 android 4.0.4 I have OutOfMemoryError problem. Logcat messages are below. Thank you.
07-13 10:42:23.820: E/AndroidRuntime(2065): FATAL EXCEPTION: main
07-13 10:42:23.820: E/AndroidRuntime(2065): java.lang.OutOfMemoryError
07-13 10:42:23.820: E/AndroidRuntime(2065): at android.graphics.Bitmap.nativeCreate(Native Method)
07-13 10:42:23.820: E/AndroidRuntime(2065): at android.graphics.Bitmap.createBitmap(Bitmap.java:605)
07-13 10:42:23.820: E/AndroidRuntime(2065): at android.graphics.Bitmap.createBitmap(Bitmap.java:551)
07-13 10:42:23.820: E/AndroidRuntime(2065): at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:437)
07-13 10:42:23.820: E/AndroidRuntime(2065): at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:618)
07-13 10:42:23.820: E/AndroidRuntime(2065): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:593)
07-13 10:42:23.820: E/AndroidRuntime(2065): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:445)
07-13 10:42:23.820: E/AndroidRuntime(2065): at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:468)
07-13 10:42:23.820: E/AndroidRuntime(2065): at com.emin.avci.SeviyorSevmiyor.onCreate(SeviyorSevmiyor.java:75)
07-13 10:42:23.820: E/AndroidRuntime(2065): at android.app.Activity.performCreate(Activity.java:4562)
07-13 10:42:23.820: E/AndroidRuntime(2065): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
07-13 10:42:23.820: E/AndroidRuntime(2065): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
07-13 10:42:23.820: E/AndroidRuntime(2065): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
07-13 10:42:23.820: E/AndroidRuntime(2065): at android.app.ActivityThread.access$600(ActivityThread.java:128)
07-13 10:42:23.820: E/AndroidRuntime(2065): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
07-13 10:42:23.820: E/AndroidRuntime(2065): at android.os.Handler.dispatchMessage(Handler.java:99)
07-13 10:42:23.820: E/AndroidRuntime(2065): at android.os.Looper.loop(Looper.java:137)
07-13 10:42:23.820: E/AndroidRuntime(2065): at android.app.ActivityThread.main(ActivityThread.java:4514)
07-13 10:42:23.820: E/AndroidRuntime(2065): at java.lang.reflect.Method.invokeNative(Native Method)
07-13 10:42:23.820: E/AndroidRuntime(2065): at java.lang.reflect.Method.invoke(Method.java:511)
07-13 10:42:23.820: E/AndroidRuntime(2065): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
07-13 10:42:23.820: E/AndroidRuntime(2065): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
07-13 10:42:23.820: E/AndroidRuntime(2065): at dalvik.system.NativeStart.main(Native Method)
ok This is code for the activity where my app is crashing.
`public class SeviyorSevmiyor extends Activity implements OnTouchListener{
private int toastX=0,toastY=0;
private int fortuneCon;
private String toastMessage;
private String categoryName;
private ImageView activityHead;
private Vibrator shake;
private Toast toast;
private View layout;
private LayoutInflater inflater ;
private ViewGroup backgForToast;
private TextView textForToast ;
private Intent resultIntent;
private int[] leafIDs={R.id.bir,R.id.iki,R.id.uc,R.id.dort,R.id.bes,R.id.alti,R.id.yedi,
R.id.sekiz,R.id.dokuz,R.id.on,R.id.onbir,R.id.oniki,R.id.onuc,R.id.ondort,R.id.onbes,R.id.onalti,R.id.iView1};
private int[] drwbles={R.drawable.leaf_1,R.drawable.leaf_2,R.drawable.leaf_3,R.drawable.leaf_4,R.drawable.leaf_5,R.drawable.leaf_6,
R.drawable.leaf_7,R.drawable.leaf_8,R.drawable.leaf_9,R.drawable.leaf_10,R.drawable.leaf_11,R.drawable.leaf_12,R.drawable.leaf_13,R.drawable.leaf_14,
R.drawable.leaf_15,R.drawable.leaf_16,R.drawable.papatya_orta};
ImageView[] leafs=new ImageView[17];
private Button leaf11_btn1,leaf11_btn2,leaf12_btn1,leaf12_btn2,leaf13_btn1,leaf13_btn2,leaf13_btn3,leaf14_btn1,leaf14_btn2,leaf15_btn1,
leaf15_btn2,leaf16_btn1,leaf1_btn1,leaf1_btn2,leaf2_btn1,leaf2_btn2,leaf3_btn1,leaf3_btn2,leaf4_btn1,leaf4_btn2,leaf5_btn1,leaf5_btn2
,leaf6_btn1,leaf6_btn2,leaf7_btn1,leaf7_btn2,leaf8_btn1,leaf8_btn2,leaf9_btn1,leaf9_btn2,leaf10_btn1,leaf10_btn2;
private Bitmap[] bmImages=new Bitmap[17];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.seviyor);
activityHead=(ImageView) findViewById(R.id.activityhead1);
shake = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
BitmapFactory.Options options=new BitmapFactory.Options();
for(int i=0;i<17;i++){
if(bmImages[i]!=null){
bmImages[i].recycle();
bmImages=null;
}
}
for(int i=0;i<17;i++){
bmImages[i]=BitmapFactory.decodeResource(getResources(),drwbles[i] , options);
}
initilizeToastBackground();
initilizeButtonsFor16Leafs();
initilize16Lisener();
resultIntent=new Intent(this,ResultFrame.class);
fortuneCon=16;
for(int i=0;i<17;i++){
leafs[i]=(ImageView) findViewById(leafIDs[i]);
}
for(int i=0;i<17;i++){
leafs[i].setImageBitmap(bmImages[i]);
}
Bundle frompreviousInent=getIntent().getExtras();
categoryName=frompreviousInent.getString("category");
setHeadOfActivity();
}
private void setHeadOfActivity() {
if(categoryName.equalsIgnoreCase("seviyor")){
activityHead.setImageResource(R.drawable.seviyor);
}
else if(categoryName.equalsIgnoreCase("konus")){
activityHead.setImageResource(R.drawable.konus);
}
else if(categoryName.equalsIgnoreCase("ekle")){
activityHead.setImageResource(R.drawable.ekle);
}
else{
activityHead.setImageResource(R.drawable.teklif_et);
}
}
private void initilize16Lisener() {
leaf11_btn1.setOnTouchListener(this);
leaf11_btn2.setOnTouchListener(this);
leaf12_btn1.setOnTouchListener(this);
leaf12_btn2.setOnTouchListener(this);
leaf13_btn1.setOnTouchListener(this);
leaf13_btn2.setOnTouchListener(this);
leaf13_btn3.setOnTouchListener(this);
leaf14_btn1.setOnTouchListener(this);
leaf14_btn2.setOnTouchListener(this);
leaf15_btn1.setOnTouchListener(this);
leaf15_btn2.setOnTouchListener(this);
leaf16_btn1.setOnTouchListener(this);
leaf1_btn1.setOnTouchListener(this);
leaf1_btn2.setOnTouchListener(this);
leaf2_btn1.setOnTouchListener(this);
leaf2_btn2.setOnTouchListener(this);
leaf3_btn1.setOnTouchListener(this);
leaf3_btn2.setOnTouchListener(this);
leaf4_btn1.setOnTouchListener(this);
leaf4_btn2.setOnTouchListener(this);
leaf5_btn1.setOnTouchListener(this);
leaf5_btn2.setOnTouchListener(this);
leaf6_btn1.setOnTouchListener(this);
leaf6_btn2.setOnTouchListener(this);
leaf7_btn1.setOnTouchListener(this);
leaf7_btn2.setOnTouchListener(this);
leaf8_btn1.setOnTouchListener(this);
leaf8_btn2.setOnTouchListener(this);
leaf9_btn1.setOnTouchListener(this);
leaf9_btn2.setOnTouchListener(this);
leaf10_btn1.setOnTouchListener(this);
leaf10_btn2.setOnTouchListener(this);
}
private void initilizeButtonsFor16Leafs() {
leaf11_btn1 = (Button) findViewById(R.id.leaf11_bnt1);
leaf11_btn2 = (Button) findViewById(R.id.leaf11_bnt2);
leaf12_btn1 = (Button) findViewById(R.id.leaf12_bnt1);
leaf12_btn2 = (Button) findViewById(R.id.leaf12_bnt2);
leaf13_btn1 = (Button) findViewById(R.id.leaf13_bnt1);
leaf13_btn2 = (Button) findViewById(R.id.leaf13_bnt2);
leaf13_btn3 = (Button) findViewById(R.id.leaf13_bnt3);
leaf14_btn1 = (Button) findViewById(R.id.leaf14_bnt1);
leaf14_btn2 = (Button) findViewById(R.id.leaf14_bnt2);
leaf15_btn1 = (Button) findViewById(R.id.leaf15_bnt1);
leaf15_btn2 = (Button) findViewById(R.id.leaf15_bnt2);
leaf16_btn1 = (Button) findViewById(R.id.leaf16_bnt1);
leaf1_btn1 = (Button) findViewById(R.id.leaf1_btn1);
leaf1_btn2 = (Button) findViewById(R.id.leaf1_btn2);
leaf2_btn1 = (Button) findViewById(R.id.leaf2_bnt1);
leaf2_btn2 = (Button) findViewById(R.id.leaf2_btn2);
leaf3_btn1 = (Button) findViewById(R.id.leaf3_btn1);
leaf3_btn2 = (Button) findViewById(R.id.leaf3_btn2);
leaf4_btn1 = (Button) findViewById(R.id.leaf4_btn1);
leaf4_btn2 = (Button) findViewById(R.id.leaf4_btn2);
leaf5_btn1 = (Button) findViewById(R.id.leaf5_btn1);
leaf5_btn2 = (Button) findViewById(R.id.leaf5_btn2);
leaf6_btn1 = (Button) findViewById(R.id.leaf6_btn1);
leaf6_btn2 = (Button) findViewById(R.id.leaf6_btn2);
leaf7_btn1 = (Button) findViewById(R.id.leaf7_btn1);
leaf7_btn2 = (Button) findViewById(R.id.leaf7_btn2);
leaf8_btn1 = (Button) findViewById(R.id.leaf8_btn1);
leaf8_btn2 = (Button) findViewById(R.id.leaf8_btn2);
leaf9_btn1 = (Button) findViewById(R.id.leaf9_btn1);
leaf9_btn2 = (Button) findViewById(R.id.leaf9_btn2);
leaf10_btn1 = (Button) findViewById(R.id.leaf10_btn1);
leaf10_btn2 = (Button) findViewById(R.id.leaf10_btn2);
}
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
toast.cancel();
this.toastX = (int) event.getRawX();
this.toastY = (int) event.getRawY();
switch (v.getId()) {
case R.id.leaf11_bnt1 :
showFortuneToast(toastX,toastY);
shake.vibrate(40);
clearBtnLeaf11();
break;
case R.id.leaf11_bnt2:
showFortuneToast(toastX,toastY);
shake.vibrate(40);
clearBtnLeaf11();
break;
case R.id.leaf12_bnt1:
showFortuneToast(toastX,toastY);
shake.vibrate(40);
clearBtnLeaf12();
break;
case R.id.leaf12_bnt2:
showFortuneToast(toastX,toastY);
shake.vibrate(40);
clearBtnLeaf12();
break;
case R.id.leaf13_bnt1:
showFortuneToast(toastX,toastY);
shake.vibrate(40);
clearBtnLeaf13();
break;
case R.id.leaf13_bnt2:
showFortuneToast(toastX,toastY);
shake.vibrate(40);
clearBtnLeaf13();
break;
case R.id.leaf13_bnt3:
showFortuneToast(toastX,toastY);
shake.vibrate(40);
clearBtnLeaf13();
break;
case R.id.leaf14_bnt1:
showFortuneToast(toastX,toastY);
shake.vibrate(40);
clearBtnLeaf14();
break;
case R.id.leaf14_bnt2:
showFortuneToast(toastX,toastY);
shake.vibrate(40);
clearBtnLeaf14();
break;
case R.id.leaf15_bnt1:
showFortuneToast(toastX,toastY);
shake.vibrate(40);
clearBtnLeaf15();
break;
case R.id.leaf15_bnt2:
showFortuneToast(toastX,toastY);
shake.vibrate(40);
clearBtnLeaf15();
break;
case R.id.leaf16_bnt1:
showFortuneToast(toastX,toastY);
shake.vibrate(40);
clearBtnLeaf16();
break;
case R.id.leaf1_btn1:
showFortuneToast(toastX,toastY);
shake.vibrate(40);
clearBtnLeaf1();
break;
case R.id.leaf1_btn2:
showFortuneToast(toastX,toastY);
shake.vibrate(40);
clearBtnLeaf1();
break;
case R.id.leaf2_bnt1:
showFortuneToast(toastX,toastY);
shake.vibrate(40);
clearBtnLeaf2();
break;
case R.id.leaf2_btn2:
showFortuneToast(toastX,toastY);
shake.vibrate(40);
clearBtnLeaf2();
break;
case R.id.leaf3_btn1:
showFortuneToast(toastX,toastY);
shake.vibrate(40);
clearBtnLeaf3();
break;
case R.id.leaf3_btn2:
showFortuneToast(toastX,toastY);
shake.vibrate(40);
clearBtnLeaf3();
break;
case R.id.leaf4_btn1:
showFortuneToast(toastX,toastY);
shake.vibrate(40);
clearBtnLeaf4();
break;
case R.id.leaf4_btn2:
showFortuneToast(toastX,toastY);
shake.vibrate(40);
clearBtnLeaf4();
break;
case R.id.leaf5_btn1:
showFortuneToast(toastX,toastY);
shake.vibrate(40);
clearBtnLeaf5();
break;
case R.id.leaf5_btn2:
showFortuneToast(toastX,toastY);
shake.vibrate(40);
clearBtnLeaf5();
break;
case R.id.leaf6_btn1:
showFortuneToast(toastX,toastY);
shake.vibrate(40);
clearBtnLeaf6();
break;
case R.id.leaf6_btn2:
showFortuneToast(toastX,toastY);
shake.vibrate(40);
clearBtnLeaf6();
break;
case R.id.leaf7_btn1:
showFortuneToast(toastX,toastY);
shake.vibrate(40);
clearBtnLeaf7();
break;
case R.id.leaf7_btn2:
showFortuneToast(toastX,toastY);
shake.vibrate(40);
clearBtnLeaf7();
break;
case R.id.leaf8_btn1:
showFortuneToast(toastX,toastY);
shake.vibrate(40);
clearBtnLeaf8();
break;
case R.id.leaf8_btn2:
showFortuneToast(toastX,toastY);
shake.vibrate(40);
clearBtnLeaf8();
break;
case R.id.leaf9_btn1:
showFortuneToast(toastX,toastY);
shake.vibrate(40);
clearBtnLeaf9();
break;
case R.id.leaf9_btn2:
showFortuneToast(toastX,toastY);
shake.vibrate(40);
clearBtnLeaf9();
break;
case R.id.leaf10_btn1:
showFortuneToast(toastX,toastY);
shake.vibrate(40);
clearBtnLeaf10();
break;
case R.id.leaf10_btn2:
showFortuneToast(toastX,toastY);
shake.vibrate(40);
clearBtnLeaf10();
break;
default:
break;
}
}
return false;
}
private void showFortuneToast(int toastX2, int toastY2) {
/**** decide which category is choosed (seviyor-sevmiyor, ekle-ekleme,konus-konuşma etc..)/ if(categoryName.equalsIgnoreCase("seviyor")){ if(fortuneCon%2==0){ toastMessage="Seviyor"; } else{ toastMessage="Sevmiyor"; } } else if(categoryName.equalsIgnoreCase("konus")){ if(fortuneCon%2==0){ toastMessage="Konuş"; } else{ toastMessage="Konuşma"; } } else if(categoryName.equalsIgnoreCase("ekle")){ if(fortuneCon%2==0){ toastMessage="Ekle"; } else{ toastMessage="Ekleme"; } } else if(categoryName.equalsIgnoreCase("teklifet")){ if(fortuneCon%2==0){ toastMessage="Teklif Et"; } else{ toastMessage="Teklif Etme"; } }
textForToast.setText(toastMessage);
toast.setGravity(Gravity.TOP|Gravity.LEFT, toastX2-90,toastY2-90);
toast.setDuration(30);
toast.setView(layout);
toast.show();
fortuneCon--;
if(fortuneCon==0){
toast.cancel();
resultIntent.putExtra("result_cond", toastMessage);
startActivity(resultIntent);
}
}
private void initilizeToastBackground() {
inflater = getLayoutInflater();
backgForToast=(ViewGroup) findViewById(R.id.toast_layout_root);
layout = inflater.inflate(R.layout.toastbackground,backgForToast);
textForToast = (TextView) layout.findViewById(R.id.text);
toast = new Toast(getApplicationContext());
}
private void clearBtnLeaf10() {
leafs[9].setVisibility(View.GONE);
leaf10_btn1.setVisibility(View.GONE);
leaf10_btn2.setVisibility(View.GONE);
}
private void clearBtnLeaf9() {
leafs[8].setVisibility(View.GONE);
leaf9_btn1.setVisibility(View.GONE);
leaf9_btn2.setVisibility(View.GONE);
}
private void clearBtnLeaf8() {
leafs[7].setVisibility(View.GONE);
leaf8_btn1.setVisibility(View.GONE);
leaf8_btn2.setVisibility(View.GONE);
}
private void clearBtnLeaf7() {
leafs[6].setVisibility(View.GONE);
leaf7_btn1.setVisibility(View.GONE);
leaf7_btn2.setVisibility(View.GONE);
}
private void clearBtnLeaf6() {
leafs[5].setVisibility(View.GONE);
leaf6_btn1.setVisibility(View.GONE);
leaf6_btn2.setVisibility(View.GONE);
}
private void clearBtnLeaf5() {
leafs[4].setVisibility(View.GONE);
leaf5_btn1.setVisibility(View.GONE);
leaf5_btn2.setVisibility(View.GONE);
}
private void clearBtnLeaf4() {
leafs[3].setVisibility(View.GONE);
leaf4_btn1.setVisibility(View.GONE);
leaf4_btn2.setVisibility(View.GONE);
}
private void clearBtnLeaf3() {
leafs[2].setVisibility(View.GONE);
leaf3_btn1.setVisibility(View.GONE);
leaf3_btn2.setVisibility(View.GONE);
}
private void clearBtnLeaf2() {
leafs[1].setVisibility(View.GONE);
leaf2_btn1.setVisibility(View.GONE);
leaf2_btn2.setVisibility(View.GONE);
}
private void clearBtnLeaf1() {
leafs[0].setVisibility(View.GONE);
leaf1_btn1.setVisibility(View.GONE);
leaf1_btn2.setVisibility(View.GONE);
}
private void clearBtnLeaf16() {
leafs[15].setVisibility(View.GONE);
leaf16_btn1.setVisibility(View.GONE);
}
private void clearBtnLeaf15() {
leafs[14].setVisibility(View.GONE);
leaf15_btn1.setVisibility(View.GONE);
leaf15_btn2.setVisibility(View.GONE);
}
private void clearBtnLeaf14() {
leafs[13].setVisibility(View.GONE);
leaf14_btn1.setVisibility(View.GONE);
leaf14_btn2.setVisibility(View.GONE);
}
private void clearBtnLeaf13() {
leafs[12].setVisibility(View.GONE);
leaf13_btn3.setVisibility(View.GONE);
leaf13_btn1.setVisibility(View.GONE);
leaf13_btn2.setVisibility(View.GONE);
}
private void clearBtnLeaf12() {
leafs[11].setVisibility(View.GONE);
leaf12_btn1.setVisibility(View.GONE);
leaf12_btn2.setVisibility(View.GONE);
}
private void clearBtnLeaf11() {
leafs[10].setVisibility(View.GONE);
leaf11_btn1.setVisibility(View.GONE);
leaf11_btn2.setVisibility(View.GONE);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
Intent menuReturn=new Intent(this,MenuFrame.class);
startActivity(menuReturn);
}
return super.onKeyDown(keyCode, event);
}
}
Edit: Android version 4.0.4 not 4.1 `
For some reason there's not enough memory for you to create a
Bitmap
. You can wrap yourBitmap
creation intotry { ... } catch(OutOfMemoryError) {}
In the catch block or elsewhere you may want to scale your bitmap down, maybe then it gets created something like
Use below method to reduce the size of the image. You are getting OutOfMemory error because of large image size.
and use in your code like
For more details, I suggest you to must read developer information provided by Android developer site on Bitmap efficiency.