2012-12-24 58 views
1

有麻烦处理java.lang.OutOfMemoryError:位图大小超过虚拟机预算错误。原始图片从未大于250x250像素。并从可绘制文件夹加载。我在互联网上发现了一些解决方案,讨论'inJustDecodeBounds',但我无法让它工作。有关如何解决此问题的任何想法?这是造成我现在两天头痛...OutOfMemoryError当加载我的GridView与图像

现在,我通过我计算基于父宽度的因素重新调整图像..

@Override 
    public View getView(int position, View v, ViewGroup parent) { 
     View mView = v; 
     this.parent = parent; 

     if (mView == null) { 

      LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
        Context.LAYOUT_INFLATER_SERVICE); 
      mView = vi.inflate(R.layout.caa_xml, null); 
     } 

     ImageView image = (ImageView) mView.findViewById(R.id.iv_caarow); 

     String name = getItem(position).getFile(); 
     int resId = C.getResources().getIdentifier(name, "drawable", 
       "com.test.com"); 
     int imageWidth = (int) calculateImageWidth(); 
     // load the origial BitMap (250 x 250 px) 
     Bitmap bitmapOrg = BitmapFactory 
       .decodeResource(C.getResources(), resId); 

     int width = bitmapOrg.getWidth(); 
     int height = bitmapOrg.getHeight(); 
     int newWidth = imageWidth; 
     int newHeight = imageWidth; 

     float scaleWidth = ((float) newWidth)/width; 
     float scaleHeight = ((float) newHeight)/height; 

     // create a matrix for the manipulation 
     Matrix matrix = new Matrix(); 
     // resize 
     matrix.postScale(scaleWidth, scaleHeight); 
     // recreate the new Bitmap 
     Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, width, 
       height, matrix, true); 

     BitmapDrawable bmd = new BitmapDrawable(resizedBitmap); 

     image.setImageDrawable(bmd); 

     if (mView != null) { 

      //additional code here 

     } 
     return mView; 
    } 

    private float calculateImageWidth() { 
     // TODO Auto-generated method stub 
     int parentW = parent.getWidth() - parent.getPaddingLeft() 
       - parent.getPaddingRight(); 
     Resources r = C.getResources(); 
     float pxPaddingBetweenItem = TypedValue.applyDimension(
       TypedValue.COMPLEX_UNIT_DIP, 2, r.getDisplayMetrics()); 
     float pxPaddingInItem = TypedValue.applyDimension(
       TypedValue.COMPLEX_UNIT_DIP, 10, r.getDisplayMetrics()); 

     int totalImageWidth = (parentW - (int) (3 * pxPaddingBetweenItem) - (int) (8 * pxPaddingInItem))/4; 
     float imageWidth = (float) totalImageWidth; 
     return imageWidth; 
    } 

回答

3

的问题是,您所创建通过使用旧的大尺寸的缩放位图。之后,你的内存中有两个Bitmaps,你甚至不回收旧内存。

无论如何,有一个更好的办法:

ImageView imageView = (ImageView) findViewById(R.id.some_id); 
String pathToImage = "path"; 

BitmapFactory.Options bmOptions = new BitmapFactory.Options(); 
bmOptions.inJustDecodeBounds = true; 
BitmapFactory.decodeFile(pathToImage, bmOptions); 
int photoW = bmOptions.outWidth; 
int photoH = bmOptions.outHeight; 

// Determine how much to scale down the image 
int scaleFactor = Math.min(photoW/50, photoH/50); 

// Decode the image file into a Bitmap sized to fill the View 
bmOptions.inJustDecodeBounds = false; 
bmOptions.inSampleSize = scaleFactor; 
bmOptions.inPurgeable = true; 

Bitmap bitmap = BitmapFactory.decodeFile(pathToFile, bmOptions); 
imageView.setImageBitmap(bitmap); 

编辑:

当你想使用一个资源ID而不是文件路径,使用decodeResource,做这样的最后一部分:

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), resourceId, bmOptions); 
imageView.setImageBitmap(bitmap); 

希望这段代码能帮助你!

+0

谢谢Chris!只有一个问题,路径,我需要把它放在一个字符串中吗?或者是否可以用int resId = C.getResources()。getIdentifier(name,“drawable”, \t \t \t \t \t“com.test.com”);并使用decodeFile(resId,...)? –

+1

编辑我的文章 - 希望能帮助你!如果是的话,请将帖子标记为正确答案,这是圣诞节;) –

+0

嘿,克里斯,我肯定会这么做的;)它现在有效!我只有一个很大的质量损失..特别是与此方法的边缘..我发现在SA上添加bmOptions.inScaled = false; \t \t bmOptions.inDither = false; \t \t bmOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;会解决这个问题,但它does not''t:s –