2012-07-13 65 views
1

我之前在我的android应用程序中询问了关于OutOfMemoryError问题的帮助。由于我还没有解决方案,但我需要再次请您给我一个解决方案。我在几款真正的Android设备上测试了我的应用但在三星Galaxy S3的Android 4.0.4我有OutOfMemoryError问题。 Logcat消息如下。谢谢。Android应用程序中的OutOfMemoryError

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这是我的应用崩溃时的活动代码。

`公共类SeviyorSevmiyor延伸活动实现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){toverseMessage =“Seviyor”; } else { toastMessage =“Sevmiyor”; (类别名称.equalsIgnoreCase(“konus”)){ 如果(fortuneCon%2 == 0){0} } else { toastMessage =“Konuşma”; (categoryName.equalsIgnoreCase(“ekle”)){ if(fortuneCon%2 == 0){0} {0} } else { toastMessage =“Ekleme”; } } 否则如果(categoryName.equalsIgnoreCase( “teklifet”)){ 如果(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); 
} 

}

编辑:Android版本4.0.4不是4.1 `

+1

显然你正试图创建一个太大的位图。你能发布你的应用程序崩溃的确切源代码吗? – 2012-07-13 07:55:30

+2

Android应用程序仅限于相对较小的堆。大的位图需要大量的内存 - 本质上是宽度*高度*颜色深度的字节数组。描述更多关于你在做什么,并粘贴一些代码 - 取决于你在做什么,你可能能够通过以更小的比例或更低的颜色深度加载位图来解决这个问题,或者... – Stevie 2012-07-13 07:56:43

+0

我有同样的问题。我结束了只是更小的图像。 – 2012-07-13 07:59:42

回答

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开发者网站上的位图效率提供developer信息。

+0

calculateInSampleSize(options,reqWidth,reqHeight)返回options.inSampleSize?在我的代码,当我设置2 options.inSampleSize应用程序工作正常..但我的图像较小,那么我需要 – Emilla 2012-07-13 09:09:31

+0

看到编辑的代码。我还附上了我从哪里获得代码的链接。方法采取reqWidth,reqHeight所以传递你想要的大小的值。 – 2012-07-13 09:41:12

+0

你说这段代码工作正常,所以接受答案。 – 2012-07-14 16:33:25

0

出于某种原因,没有足够的内存,为您创造一个Bitmap。你可以用你的Bitmap创作到try { ... } catch(OutOfMemoryError) {}

在catch块中或其他地方,你可能想扩展您的位图了,也许那时它就会产生类似下面的方法

bmp = Bitmap.createBitmap(bmp, bmp.getWidth()/2, bmp.getheight()/2, true)