Android BitmapFactory 通过互联网
我从互联网下载图像,然后在屏幕上缩放显示它们。这个想法是使用 inSampleSize,这样我可以在解压缩时缩放大图像并防止 OutOfMemory 异常。但为了找到 inSampleSize 我需要知道图像分辨率。它可以使用 inJustDecodeBounds 选项获得。
问题是我无法传递流并直接从互联网下载图像,因为它将被下载两次(首先获取大小,然后获取缩放后的位图)。我无法下载图像并将其存储在 RAM 中,因为图像大小可能很大。剩下的唯一解决方案是将图像下载到 SD 卡/内存并从那里读取,但是当用户没有剩余空间时,就会发生不好的事情。
问题是 - 有没有什么办法可以做到这一点,而不依赖于存储和 RAM 内存,不需要图像下载两次?或者 BitmapFactory 在使用 inJustDecodeBounds 时可能不会下载整个图像,而只是下载标题?
谢谢
I'm downloading images from the internet and then displaying them scaled on the screen. The idea is to use inSampleSize so I can scale large images while decompressing and prevent OutOfMemory exception. But in order to find inSampleSize I need to know image resolution. It can be obtained using inJustDecodeBounds option.
The problem is that I can't pass it the stream and download the image from the internet directly, because it will be downloaded twice (first to get the size, then to get the scaled Bitmap). I can't download the image and store it in RAM, because image size may be large. The only solution left is to download the image to SD card / internal memory and read it from there, but when user has no space left bad things are going to happen.
The question is - is there any way to do it without relying on the storage and RAM memory which doesn't require the image to be downloaded twice? Or maybe BitmapFactory doesn't download whole image when it's using inJustDecodeBounds, but just headers?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您下载的是 PNG、GIF 或 JPG,则应该可以读取文件标题来确定宽度和高度,这意味着您无需下载整个文件。
这篇文章有一些用于读取宽度和高度的 C# 代码从上述格式的标头中可以将其移植到 Java 以在 Android 中使用。
If you're downloading a PNG, GIF or JPG it should be possible to read the file headers to determine the width and height, which should mean you avoid having to download the entire file.
This post has some C# code for reading the width and height from the headers for the above format which you could port to Java for use in Android.