2011-11-28 135 views
1

我正在實施一個應用程序,我將圖像附加到問題中。這些附件有兩種類型,使用的相機從畫廊的照片 Android圖像處理

  • 附件

    1. 附件(已經存在於圖庫照片)

    我讓你圖片如下

    //Camera Request 
    Intent captureImage = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
    
    captureImage.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageFileUri); 
    startActivityForResult(captureImage, CAMERA_PIC_REQUEST); 
    
    // Gallery Pic request 
    Intent intent = new Intent(Intent.ACTION_PICK, 
         android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
         startActivityForResult(intent, GALLERY_PIC_REQUEST); 
    

    以上的結果是URI。我將這個uri.getPath()保存在我的數據庫中。

    現在的問題是,當我想要顯示這些圖像我正在使用uri 我正在抓取它。但我第一次加載圖像 java.lang.OutOfMemoryError後得到以下異常:位圖大小超過VM預算

    我讀了一些博客,我才知道,內存是不夠的加載它。

    是否有任何人在列表中顯示壓縮圖像時有工作解決方案。 工作完成後使用回收存儲器。

  • 回答

    1

    您可以使用以下方法:

    BitmapFactory.Options options; 
    options = new BitmapFactory.Options(); 
    options.inSampleSize = 4; 
    options.inTempStorage = new byte[16 * 1024]; 
    Bitmap bm = BitmapFactory.decodeFile(pathToFile, options); 
    

    這將加載只有一個圖像中的像素的四分之一。將inSampleSize值更改爲加載/更少。

    0
    Display display = getWindowManager().getDefaultDisplay(); 
    
                 int width = display.getWidth(); 
                 int height = display.getHeight(); 
    
    
                 int bitmap_width = photoBitmap.getWidth(); 
                 int bitmap_height = photoBitmap.getHeight(); 
        // where photoBitmap is your Bitmap image         
    
                 if(bitmap_width >width) 
                  bitmap_width = width ; 
    
                 if(bitmap_height>height) 
                 bitmap_height = height ; 
    
    
    // compressing bitmap goes here 
                 photoBitmap = Bitmap.createScaledBitmap (photoBitmap, width, height, false);