CreateScaledBitmap导致Grow堆

时间:2014-08-31 09:42:47

标签: android multithreading bitmap camera bitmapimage

我使用我在Handler线程上打开的自定义相机来拍摄我的jpeg回调。

我希望在缩小图片后将图片存储在缓存中,以便图片适合1000 x 1000尺寸保持其比例。

为此,我使用以下方式从jpeg回调中拍摄照片:

private Camera.PictureCallback jpegCallback = new Camera.PictureCallback() {
    public void onPictureTaken(byte[] data, final Camera camera) {

        Bitmap realImage;
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 4;
        options.inPurgeable=true;                  
        options.inInputShareable=true;          
        realImage = BitmapFactory.decodeByteArray(data,0,data.length,options);
    }

到目前为止,我可以拍摄尽可能多的照片,没有记忆问题...

但是当我想将图片写入缓存时,我需要使用以下方式缩小图片:

private class addThumbnailASYNC extends AsyncTask<Bitmap, Void, Bitmap> {
    @Override
    protected Bitmap doInBackground(Bitmap... bitmap) {

        Bitmap image = bitmap[0]; 
        int width = image.getWidth();
        int height = image.getHeight();

        float bitmapRatio = (float)width / (float) height;
        if (bitmapRatio > 0) {
            width = MAX_SIZE_IMAGE;
            height = (int) (width / bitmapRatio);
        } else {
            height = MAX_SIZE_IMAGE;
            width = (int) (height * bitmapRatio);
        }

        image = Bitmap.createScaledBitmap(image, width, height, true);  

    }

问题出在我使用的时候 image = Bitmap.createScaledBitmap 我得到一个Grow堆,如果我拍很多照片,我认为会导致内存不足。

有三件事我不明白:

  • 当我使用options.inSampleSize时,图片应缩放4,那么为什么createScaledBitmap会导致增长堆?
  • 如果我不使用createScaledBitmap并且我将图像写入缓存,我从来没有得到增长堆为什么? inSampleSize应该给我比使用scaledBitmap创建的文件更重的文件?
  • 我需要避免内存堆,但是如何避免增长堆因为我没有选择使用createdScaledBitmap而且我的图片将始终被相机拍摄,我感到卡住了。

0 个答案:

没有答案