1

对于 Android,在将图像加载到内存中以确保我们不会超出我们通常执行的 RAM 限制

BitmapFactory.Options bounds = new BitmapFactory.Options();
bounds.inJustDecodeBounds = true;

然后解码位图以找到它的宽度,我们可以根据它来决定是否采样。

我的问题是,如果我们从本地存储(SD 卡)加载位图,我们不能得到位图的文件位置:

   File file = new File(mPathToFile);

然后检查

   if(file.length()>MAX){

   sampleSize = 2;//2 is just an example
   BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = sampleSize;
    bitmap = BitmapFactory.decodeFile(mPathToFile, options);
    }

这是错的吗?愚蠢的?

4

3 回答 3

2

Is this wrong? Stupid?

No, but it might be too inaccurate. The file size in bytes will not usually represent the actual image size in memory because image files like JPG and PNG are compressed representations of the Bitmap. So the file size is not directly linked to the image size (while they are proportional, you can't necessarily say that a 50K JPG file will be 250x250 pixels).

This also means that 50KB on disk does not mean 50KB once inflated into a Bitmap in memory, because it is compressed. The image itself will likely take closer to 300KB (or more) in memory (the requirements are roughly W * H * 4 bytes for an ARGB_8888 Bitmap).

Doing a bounds decode allows you to make this decision based on the actual size the image will be (and thus how much memory it will actually take up in your program).

于 2012-12-02T19:25:31.300 回答
1

我了解用于处理位图的堆内存与图像分辨率有关,而不是与文件的物理大小/长度有关。

我的理解是,分辨率为 1280 x 800 的图像 A 使用/需要比 640 x 400 的图像 B 更多的堆。并且根据质量文件大小 B 可能大于 A。如果我错了,请纠正我

于 2012-12-02T19:19:17.397 回答
0

Android docs on similar topic Loading Large Bitmaps Efficiently

于 2012-12-02T19:38:59.237 回答