我有一个大约 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个字节吗?或者还有什么我需要知道的吗?