2011-11-01 68 views
0

我已經構建了一個用戶可以拍照的自定義相機活動。圖片通過圖片回調保存在外部存儲目錄中。用戶然後被重定向到可以預覽圖片的另一個活動。在顯示預覽之前,圖片會在單獨的線程中縮小到某個最大尺寸。下面是縮放代碼:Android Desire HD位圖縮放

byte[] tempStorage = new byte[16 * 1024]; 
BitmapFactory.Options options = new BitmapFactory.Options(); 
options.inJustDecodeBounds = true; 
BitmapFactory.decodeFile(imagePath, options); 

int oldWidth = options.outWidth; 
int oldHeight = options.outHeight; 

if (oldWidth > MAX_DIM || oldHeight > MAX_DIM) { 

    Logger.i(Constants.SCALE_TAG, "Must scale picture"); 
    int scaleFactor = getScaleFactor(oldWidth, oldHeight); 
    options.inJustDecodeBounds = false; 
    options.inSampleSize = scaleFactor; 
    options.inTempStorage = tempStorage; 
    System.gc(); 
    newB = BitmapFactory.decodeFile(imagePath, options); 

} else { 

    Logger.i(Constants.SCALE_TAG, "No need for scaling"); 

    // We do not need to scale the picture, it is small 
    // enough. 
    System.gc(); 
    options.inJustDecodeBounds = false; 
    options.inTempStorage = tempStorage; 
    if (!stop) { 
     newB = BitmapFactory.decodeFile(imagePath, options); 
    } else { 
     return; 
    } 

} 

這裏是計算比例因子代碼:

private int getScaleFactor(int oldWidth, int oldHeight) { 

    // Holds the raw scale factor. 
    float rawScale; 

    // Get the scaling factor. 
    if (oldWidth >= oldHeight) { 
     rawScale = 1.0F/((MAX_DIM * 1.0F)/(oldWidth * 1.0F)); 
    } else { 
     rawScale = 1.0F/((MAX_DIM * 1.0F)/(oldHeight * 1.0F)); 
    } 
    Logger.i(Constants.SCALE_TAG, "Raw scale factor: " + rawScale); 
    return Math.round(rawScale); 

} 

此代碼工作正常上大多數設備,但不知何故的Desire HD將無法正確縮放圖片。它看起來像是拍攝了原始圖片並將其複製了30次左右,並將其縮放到位圖中,導致出現了奇怪的條紋狀圖片。有誰知道這個問題?

回答

0

您應該使用較少的內存。問題是你使用的內存太多了。