0

我有一个大约 4.19 MB(磁盘大小)的图像(3648x2736),我想将它加载到我的应用程序中,但由于内存不足,它应该崩溃了。所以为了避免这种崩溃,我在解码图像之前放置了一个验证器(不,我不想用inSampleSize它来使它更小)。

    long maxMemory = Runtime.getRuntime().maxMemory();
    long nativeUsage = Debug.getNativeHeapAllocatedSize();
    long heapSize = Runtime.getRuntime().totalMemory();
    long heapRemaining = Runtime.getRuntime().freeMemory();
    long memoryLeft = maxMemory - (heapSize - heapRemaining) - nativeUsage;


    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filePath, options);
    int bitmapSize = options.outHeight * options.outWidth * 4;

    if (bitmapSize < memoryLeft)
        return BitmapFactory.decodeFile(filePath);

现在我要确定的一件事是,我计算bitmapSize正确吗?因为图像文件大小只有 4.19 MB 并且memoryLeft超过 8 MB,但应用程序崩溃了。这意味着它将每个像素存储为 4 个字节(PNG),对吗?那么jpeg不应该是3个字节吗?或者还有什么我需要知道的吗?

4

1 回答 1

1

Since Bitmap is just a set of uncompressed pixels no matters what format it is - png or jpeg or else. Only factor you should remember is Bitmap.Config which describe color scheme for bitmap. For example Config.RGB_565 will take 2 bytes per pixel (5 bit red, 6 bit green, 5 bit green channel) and Config.ARGB_8888 will take 4 bytes per pixel (8 bits per each channel).
You can set Bitmap.Config while decoding image using BitmapFactory.Options.inPreferredConfig but as I understood from BitmapFactory.Options docs this is not guaranteed.

于 2013-08-14T09:24:22.723 回答