2011-08-18 93 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。 –

相關問題