1

我之前曾在我的 android 应用程序中寻求过有关 OutOfMemoryError 问题的帮助。由于我还没有解决方案,所以我需要再次请您为我提供解决方案。我在几个真正的安卓设备上测试了我的应用程序。但在三星 Galaxy S3 android 4.0.4 我有 OutOfMemoryError 问题。Logcat 消息如下。谢谢你。

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)

好的,这是我的应用程序崩溃的活动的代码。

`公共类 SeviyorSevmiyor 扩展 Activity 实现 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) {   

/ * ***决定选择哪个类别(seviyor-sevmiyor、ekle-ekleme、konus-konuşma 等)/ if(categoryName.equalsIgnoreCase("seviyor")){ if(fortuneCon%2==0){ toastMessage="Seviyor"; } 其他{ toastMessage="Sevmiyor"; } } else if(categoryName.equalsIgnoreCase("konus")){ if(fortuneCon%2==0){ toastMessage="Konuş"; } 其他{ toastMessage="Konuşma"; } } else if(categoryName.equalsIgnoreCase("ekle")){ if(fortuneCon%2==0){ toastMessage="Ekle"; } 其他{ toastMessage="Ekleme"; } } 否则 if(categoryName.equalsIgnoreCase("teklifet" )){ if(fortuneCon%2==0){ toastMessage="Teklif Et"; } 其他{ 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);
}

}

编辑:Android 版本 4.0.4 不是 4.1 `

4

2 回答 2

1

使用下面的方法来减小图像的大小。由于图像尺寸过大,您会收到 OutOfMemory 错误。

public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
        int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(res, resId, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeResource(res, resId, options);
}

public static int calculateInSampleSize(
            BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {
        if (width > height) {
            inSampleSize = Math.round((float)height / (float)reqHeight);
        } else {
            inSampleSize = Math.round((float)width / (float)reqWidth);
        }
    }
    return inSampleSize;
}

并在您的代码中使用

mImageView.setImageBitmap(
    decodeSampledBitmapFromResource(getResources(), R.id.myimage, 100, 100));

有关更多详细信息,我建议您必须阅读Android开发者网站提供的关于 Bitmap 效率的开发者信息。

于 2012-07-13T08:39:32.793 回答
0

由于某种原因,您没有足够的内存来创建Bitmap. 您可以将您的Bitmap创作包装成try { ... } catch(OutOfMemoryError) {}

在 catch 块或其他地方,您可能希望缩小位图,然后它可能会创建类似

bmp = Bitmap.createBitmap(bmp, bmp.getWidth()/2, bmp.getheight()/2, true)
于 2012-07-13T07:55:42.667 回答