2011-08-18 89 views
0

我正在加载图像缩放至屏幕尺寸,之后用户可以用手在其上绘制文本。加载使用相机拍摄的图像时内存不足

之后,用户可以将其保存到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);** 

回答

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; 
} 
+0

您好,感谢您的回复,我用你的方法。我只是用150,150而不是我的真实高度和宽度。但它是例外,并且所有都是由相机拍摄的图像,所以我不能有任何借口。 –

+0

通过给150 150我们正在降低屏幕上显示的图像的质量,但原始图像的质量将是相同的,它不会被改变。我不知道为什么你会得到例外。请分享例外的logcat。 –

+0

你好,感谢您的回复。我无法提供150,150,因为我必须使图像的宽度和高度与原始图像相同。以下是异常的logcat。 –

相关问题