在Android中显示SD卡中的大图像
在 Android 中,如何显示 SD 卡中的图像(任意大小),不会出现内存不足错误?
是否需要先将图片放入Media Store?
伪代码示例将不胜感激。 如果显示的图像与设备的内存水平一样大,则加分。
In Android, how do you display an image (of any size) from the SD card, without getting an out of memory error?
Is it necessary to put the image in the Media Store first?
A pseudo-code example would be greatly appreciated. Extra points if the displayed image is as big as the memory level of the device allows.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
编辑:这个问题实际上已经在 加载时出现奇怪的内存不足问题得到了解答图像到位图对象(得票最高的两个答案)。它也使用
inSampleSize
选项,但有一个小方法来自动获取适当的值。我原来的答案:
BitmapFactory 的
类可以解决您的问题(http: //developer.android.com/reference/android/graphics/BitmapFactory.Options.html#inSampleSize)。它的工作原理是生成一个位图,其宽度和高度比原始值高 1/inSampleSize
.OptionsinSampleSize
,从而减少内存消耗(通过inSampleSize
^2?)。您应该在使用之前阅读该文档。示例:
但是,我猜它仅适用于
inSampleSize
^2 倍允许内存大小的图像,并且会降低小图像的质量。诀窍是找到合适的 inSampleSize。
Edit: this question has actually been already answered at Strange out of memory issue while loading an image to a Bitmap object (the two highest voted answers). It does also use the
inSampleSize
option, but with a small method to automatically get the appropriate value.My original answer:
The
inSampleSize
of theBitmapFactory.Options
class can solve your issue (http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html#inSampleSize). It works by making a bitmap with the resulting width and height 1/inSampleSize
than the original, thus reducing memory consumption (byinSampleSize
^2?). You should read the doc before using it.Example:
However here it will only work for images up to, I guess,
inSampleSize
^2 times the size of the allowed memory and will reduce the quality of small images.The trick would be to find the appropriate inSampleSize.
我使用代码显示任何尺寸的图像:
I'm displaying any sized images using code:
例如:
For example:
http://www .developer.com/ws/other/article.php/3748281/Working-with-Images-in-Googles-Android.htm 涵盖了您需要了解的有关图像主题的所有信息,包括获取图像从 SD 卡。您会注意到执行此操作的代码,复制如下:
http://www.developer.com/ws/other/article.php/3748281/Working-with-Images-in-Googles-Android.htm covers all you'll ever need to know on the subject of images, including getting them from an SD card. You'll notice the code to do so there, copied below: