在 Android 中解码大小合适的位图 [英] Decoding bitmaps in Android with the right size

查看:19
本文介绍了在 Android 中解码大小合适的位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 BitmapFactory.decodeFile 从 SD 卡解码位图.有时位图比应用程序需要的或堆允许的大,所以我使用 BitmapFactory.Options.inSampleSize 来请求子采样(较小的)位图.

I decode bitmaps from the SD card using BitmapFactory.decodeFile. Sometimes the bitmaps are bigger than what the application needs or that the heap allows, so I use BitmapFactory.Options.inSampleSize to request a subsampled (smaller) bitmap.

问题是平台没有强制执行 inSampleSize 的确切值,有时我最终得到的位图要么太小,要么对于可用内存来说仍然太大.

The problem is that the platform does not enforce the exact value of inSampleSize, and I sometimes end up with a bitmap either too small, or still too big for the available memory.

来自 http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html#inSampleSize:

注意:解码器将尝试满足这个请求,但产生的位图可能有不同的维度正是所要求的.此外,2 的幂通常是解码器更快/更容易荣誉.

Note: the decoder will try to fulfill this request, but the resulting bitmap may have different dimensions that precisely what has been requested. Also, powers of 2 are often faster/easier for the decoder to honor.

我应该如何从 SD 卡解码位图以获得所需大小的位图,同时消耗尽可能少的内存来解码?

How should I decode bitmaps from the SD card to get a bitmap of the exact size I need while consuming as little memory as possible to decode it?

当前源代码:

BitmapFactory.Options bounds = new BitmapFactory.Options();
this.bounds.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, bounds);
if (bounds.outWidth == -1) { // TODO: Error }
int width = bounds.outWidth;
int height = bounds.outHeight;
boolean withinBounds = width <= maxWidth && height <= maxHeight;
if (!withinBounds) {
    int newWidth = calculateNewWidth(int width, int height);
    float sampleSizeF = (float) width / (float) newWidth;
    int sampleSize = Math.round(sampleSizeF);
    BitmapFactory.Options resample = new BitmapFactory.Options();
    resample.inSampleSize = sampleSize;
    bitmap = BitmapFactory.decodeFile(filePath, resample);
}

推荐答案

您走在正确的轨道上,但是您正在尝试同时做两件事:读入文件并将其缩放到适当的大小.

You are on the right track, however you are trying to do two things at once: read the file in and scale it to the appropriate size.

第一步是将文件读取到比您需要的稍大的位图,使用 BitmapFactory.Options.inSampleSize 确保当您只需要较小的缩略图或屏幕分辨率时,不会消耗过多的内存来读取大位图图像.

The first step is to read the file to a Bitmap slightly bigger than you require, using BitmapFactory.Options.inSampleSize to ensure that you do not consume excessive memory reading a large bitmap when all you want is a smaller thumbnail or screen resolution image.

第二步是调用 Bitmap.createScaledBitmap() 来创建一个新的位图到你需要的精确分辨率.

The second step is to call Bitmap.createScaledBitmap() to create a new bitmap to the exact resolution you require.

确保在临时位图之后进行清理以回收其内存.(要么让变量超出范围并让 GC 处理它,要么在加载大量图像并且内存紧张时调用 .recycle() .)

Make sure you clean up after the temporary bitmap to reclaim its memory. (Either let the variable go out of scope and let the GC deal with it, or call .recycle() on it if you are loading lots of images and are running tight on memory.)

这篇关于在 Android 中解码大小合适的位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆