2012-12-24 115 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 –