2011-11-01 69 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

您应该使用较少的内存。问题是你使用的内存太多了。