2012-12-07 62 views
0

我试图设置图像作为背景(PiePlot),但我得到OutOfMemory异常。设置背景时发生OutOfMemory错误

bg图片大小为170kb。
我尝试了5kb的背景样本图片,它毫无例外地工作。

我尝试以下操作:

@Override 
protected void onDestroy() { 
    super.onDestroy(); 

    unbindDrawables(mView); 
    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(); 
    } 
} 

但如果/当叫onDestroy()这是非常有用的。但是,当启动应用程序时,这将不起作用,因此应用程序崩溃。

我想这也:

BitmapDrawable bitmapDrawable = (BitmapDrawable) ctx.getResources().getDrawable(R.drawable.bg2); 
BitmapFactory.Options bitopt = new BitmapFactory.Options(); 
bitopt.inSampleSize = 10; 
plot.setBackgroundImage(bitmapDrawable); //plot is PiePlot object 

但相同的结果,即应用程序崩溃。

任何帮助表示赞赏。

回答

1

只是实现这个UR图像...这将增加4倍

public static Bitmap getImage(byte[] image) { 
     BitmapFactory.Options config = new BitmapFactory.Options(); 
     config.inPreferredConfig = Bitmap.Config.RGB_565; 
     config.inSampleSize = 4; 
     return BitmapFactory.decodeByteArray(image, 0, image.length,config); 

    } 
+0

什么参数应该传递给这个方法?我有'BitmapDrawable'对象....以及如何决定** inSampleSize Factor **,你在这里建议'4' ... – GAMA

+0

inSampleSize值意味着你的图像会减少***倍....这里我给4这意味着我的图像会减少4倍到实际图像,你必须通过你的图像作为参数 –

+0

传递你的bimap图像...... –

1

尝试把这个功能...

public static Bitmap decodeFile(File f,int WIDTH,int HIGHT){ 
    try { 
     //Decode image size 
     BitmapFactory.Options o = new BitmapFactory.Options(); 
     o.inJustDecodeBounds = true; 
     BitmapFactory.decodeStream(new FileInputStream(f),null,o); 

     //The new size we want to scale to 
     final int REQUIRED_WIDTH=WIDTH; 
     final int REQUIRED_HIGHT=HIGHT; 
     //Find the correct scale value. It should be the power of 2. 
     int scale=1; 
     while(o.outWidth/scale/2>=REQUIRED_WIDTH && o.outHeight/scale/2>=REQUIRED_HIGHT) 
      scale*=2; 

     //Decode with inSampleSize 
     BitmapFactory.Options o2 = new BitmapFactory.Options(); 
     o2.inSampleSize=scale; 
     return BitmapFactory.decodeStream(new FileInputStream(f), null, o2); 
    } catch (FileNotFoundException e) {} 
    return null; 
} 
+0

减少乌尔图像什么传递的3个参数? – GAMA

+0

chk接受答案http://stackoverflow.com/questions/10314527/caused-by-java-lang-outofmemoryerror-bitmap-size-exceeds-vm-budget ...我应该通过什么文件对象? – GAMA