2011-11-28 136 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);