0

我正在通过 android camera API 拍照:

为了计算某些图像处理的可用内存,我想检查图像是否适合内存。我正在使用这些功能执行此操作:

   /**
     * Checks if a bitmap with the specified size fits in memory
     * @param bmpwidth Bitmap width
     * @param bmpheight Bitmap height
     * @param bmpdensity Bitmap bpp (use 2 as default)
     * @return true if the bitmap fits in memory false otherwise
     */
    public static boolean checkBitmapFitsInMemory(long bmpwidth,long bmpheight, int bmpdensity ){
        long reqsize=bmpwidth*bmpheight*bmpdensity;
        long allocNativeHeap = Debug.getNativeHeapAllocatedSize();

        if ((reqsize + allocNativeHeap + Preview.getHeapPad()) >= Runtime.getRuntime().maxMemory())
        {
            return false;
        }
        return true;
    }

    private static long getHeapPad(){
        return (long) Math.max(4*1024*1024,Runtime.getRuntime().maxMemory()*0.1);
    }

问题是:我仍然收到 OutOfMemoryExceptions(不是在我的手机上,而是来自已经下载了我的应用程序的人)

异常发生在以下代码的最后一行:

public void onPictureTaken(byte[] data, Camera camera) {
        Log.d(TAG, "onPictureTaken - jpeg");
        final byte[] data1 = data;
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        options.inSampleSize = downscalingFactor;
        Log.d(TAG, "before gc");
        printFreeRam();
        System.gc();
        Log.d(TAG, "after gc");
        printFreeRam();
        Bitmap photo = BitmapFactory.decodeByteArray(data1, 0, data1.length, options);

downscalingFactor 是通过 checkBitmapFitsInMemory() 方法选择的。我这样做是这样的:

 for (downscalingFactor = 1; downscalingFactor < 16; downscalingFactor ++) {
    double width = (double) bestPictureSize.width / downscalingFactor;
    double height = (double) bestPictureSize.height / downscalingFactor;
    if(Preview.checkBitmapFitsInMemory((int) width, (int) height, 4*4)){ // 4 channels (RGBA) * 4 layers
        Log.v(TAG, "  supported: " + width+'x'+height);
        break;
    }else{
        Log.v(TAG, "  not supported: " + width+'x'+height);
    }
   }

任何人都知道为什么这种方法如此错误?

4

1 回答 1

0

尝试改变这个:

Bitmap photo = BitmapFactory.decodeByteArray(data1, 0, data1.length, options);

对此:

Bitmap photo = BitmapFactory.decodeByteArray(**data**, 0, data.length, options);
于 2012-07-02T22:39:28.427 回答