加载相机拍摄的图像时内存不足

时间:2011-08-18 11:49:06

标签: android

我正在加载图像,将其缩放到屏幕尺寸,之后用户可以用手绘制一些文字。

之后用户可以将其保存到SD卡。我需要将其保存为与原始图像相同的宽度和高度。对于其他样品图像,它工作正常。但是当我加载相机拍摄的图像时,我画了一些东西。我尝试将图像缩放回原始高度和宽度,但是它为相机捕获的所有图像提供了“超出内存异常”的异常。代码如下:

private void saveView(View view) {
        Bitmap b = Bitmap.createBitmap(view.getWidth(), view.getHeight(),
                Bitmap.Config.ARGB_8888);
        String filePath="/sdcard/";
        Canvas c = new Canvas(b);


        view.draw(c);
        float scaleHeight=((float)getHeight()/b.getHeight());
        float scaleWidth=((float)getWidth()/b.getWidth());
        Matrix matrix=new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        Log.e("getWidth()", ":"+getWidth());
        Log.e("getHeight()", ":"+getHeight());

              **//Giving Exception at below Line as i am trying to scat it to origenal size.
        b=Bitmap.createBitmap(b,0,0,b.getWidth(),b.getHeight(),matrix,true);**

1 个答案:

答案 0 :(得分:0)

您需要调整位图大小,如下所示。+

Bitmap resizedBitmap = getResizedBitmap(myBitmap, 150, 150);

和方法

    public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
    int width = bm.getWidth();
    int height = bm.getHeight();
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;

    // create a matrix for the manipulation
    Matrix matrix = new Matrix();
    // resize the bit map
    matrix.postScale(scaleWidth, scaleHeight);
    // recreate the new Bitmap
    Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height,
                    matrix, false);
    return resizedBitmap;
}