0

Androidカメラ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 に答える