Android:替代 GIF 加载器?

发布于 2024-12-11 21:58:25 字数 799 浏览 0 评论 0原文

我想在 Android 中加载图像,但如果图像太大,我想在加载之前调整其大小。我了解到,可以使用 BitmapFactory 来获取图像的大小,然后可以找出适当的缩放因子并使用 BitmapFactory 加载具有所需尺寸的图像(根据我发现的非常有能力的建议 在此相关线程中)。

所以我尝试了一下,但它不起作用。我花了最后一个小时浏览代码,试图找出为什么这样一个简单的操作没有任何效果(比例因子被完全忽略了!)

然后我偶然发现了 Android 问题 3072...事实证明,该问题已被认定为 GIF 文件损坏两年多了。我意识到 GIF 并不完全是现代的,但它仍然存在并被广泛使用(我的测试集有很多 GIF,这就是为什么在我发现那个错误报告之前它似乎都被破坏了)。

我的问题是,我可以使用什么来替代 BitmapFactory,以便在使用缩放因子时正确读取 GIF 文件并调整其大小,而不会默默地忽略它? 内存限制妨碍直接加载大图像以进行进一步处理,所以这需要是一个流解决方案。

我的另一个问题是,有多少其他人遇到了这样的旧的、未修复的错误,要么没有意识到,要么更糟糕的是,依赖 API 来准确执行请求的功能,最终导致内存泄漏和其他奇怪的意外?

I want to load images in Android, but if the image is just too large I wanted to resize it prior to loading . I learned that one can use the BitmapFactory to get just the size of the image, then one can figure out an appropriate scaling factor and use BitmapFactory to load the image with the required dimensions (per the very competent suggestions I found in this related thread).

So I tried that and it refused to work. I spent the last hour picking through the code trying to figure out why such a simple operation wasn't having any effect whatsoever (the scaling factor was being utterly ignored!)

Then I stumbled upon Android issue 3072... turns out this has been identified as broken for GIF files for well over two years. I realize GIF isn't exactly modern but it's still out there and in wide use (my test set has a lot of them, which is why it seemed uniformly broken until I found that bug report).

My question is, what can I use as an alternative to BitmapFactory that will correctly read and resize a GIF file when a scaling factor is employed without silently ignoring it? Memory constraints preclude loading large images directly for further processing, so this would need to be a streaming solution.

My other question is, how many others run into old, unfixed bugs like this and either don't realize it or worse, rely on the API to accurately perform the requested function and end up with memory leaks and other strange surprises?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人间☆小暴躁 2024-12-18 21:58:25

我最终使用了此处发布的解决方案。

options.inSampleSize = 2;
bitmap = BitmapFactory.decodeStream(new URL(url).openStream(),null, options);
Bitmap scaledBitmap = scaleDown(bitmap, 1280, true);
bitmap = scaledBitmap;

其中 scaleDown 是:

public static Bitmap scaleDown(Bitmap realImage, float maxImageSize,
                               boolean filter) {
    float ratio = Math.min(
                (float) maxImageSize / realImage.getWidth(),
                (float) maxImageSize / realImage.getHeight());
    int width = Math.round((float) ratio * realImage.getWidth());
    int height = Math.round((float) ratio * realImage.getHeight());

    Bitmap newBitmap = Bitmap.createScaledBitmap(realImage, width,
                height, filter);
    return newBitmap;
}

此处所使用。

I ended up using the solution posted here.

options.inSampleSize = 2;
bitmap = BitmapFactory.decodeStream(new URL(url).openStream(),null, options);
Bitmap scaledBitmap = scaleDown(bitmap, 1280, true);
bitmap = scaledBitmap;

Where scaleDown is:

public static Bitmap scaleDown(Bitmap realImage, float maxImageSize,
                               boolean filter) {
    float ratio = Math.min(
                (float) maxImageSize / realImage.getWidth(),
                (float) maxImageSize / realImage.getHeight());
    int width = Math.round((float) ratio * realImage.getWidth());
    int height = Math.round((float) ratio * realImage.getHeight());

    Bitmap newBitmap = Bitmap.createScaledBitmap(realImage, width,
                height, filter);
    return newBitmap;
}

as used here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文