2013-04-10 19 views
0

研究之後,我只能找InSampleSize來調整位圖如何調整大小的一個bimap的不createBitmap避免OOM問題

,但我尋找的東西會允許我來調整位圖或大或小的取決於屏幕分辨率

因爲bitmap.createBitmap將導致OOM,我不得不使用別的東西......

請幫助

這裏是我調整我的位圖,其中5MB結果10MB〜撞擊每個代碼當我調整一個ybitmap

Bitmap createdBitmap = Bitmap.createScaledBitmap(bitmap, newWidth, newHeight, true); 

感謝

回答

1

,你可以用它來獲取當前運行的屏幕:

@Override 
public void onSizeChanged(int w, int h, int oldw, int oldh) 
{ 
    super.onSizeChanged(w, h, oldw, oldh); 
    screenW = w; 
    screenH = h; 
} 

然後用這個,所以你被加載到內存合理地加載位圖:

BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inJustDecodeBounds = true; 
     BitmapFactory.decodeFile(selectedImagePath, options); 
     int imageHeight = options.outHeight; 
     int imageWidth = options.outWidth; 
     String imageType = options.outMimeType; 
     if(imageWidth > imageHeight){ 
      options.inSampleSize = calculateInSampleSize(options,screenH,screenW); 

     }else{ 
      options.inSampleSize = calculateInSampleSize(options,screenW,screenH); 

     } 
     options.inJustDecodeBounds = false; 
     photo = BitmapFactory.decodeFile(selectedImagePath,options); 

方法:

public 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) { 

    // Calculate ratios of height and width to requested height and width 
    final int heightRatio = Math.round((float) height/(float) reqHeight); 
    final int widthRatio = Math.round((float) width/(float) reqWidth); 

    // Choose the smallest ratio as inSampleSize value, this will guarantee 
    // a final image with both dimensions larger than or equal to the 
    // requested height and width. 
    inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; 
} 

return inSampleSize; 
} 
+0

如果我需要比原圖大小大PIC .. – supermantoby 2013-04-10 02:28:14

+0

你想要做什麼只記得,我認爲每個應用程序的堆內存16MB它,說一個普通camerashot是一個很大的內存作爲一個完整的位圖,說該設備是480X320加載,而不是完整的水庫,太多youll超載 – JRowan 2013-04-10 02:34:17

+0

inSampleSize不能拉伸位圖..否則我將能夠使用它... – supermantoby 2013-04-10 02:42:25