在Android中显示来自SD卡的大图像

hpi*_*que 7 android bitmap out-of-memory mediastore

在Android中,如何从SD卡显示图像(任何大小),而不会出现内存不足错误

是否有必要先将图像放入Media Store?

非常感谢伪代码示例.如果显示的图像与设备的内存级别一样大,则为额外点.

Gau*_*oun 13

编辑:在将图像加载到Bitmap对象(两个最高投票答案)时,这个问题实际上已经在Strange内存不足问题上得到解答.它也使用该inSampleSize选项,但使用一个小方法来自动获取适当的值.

我的原始答案:

inSampleSize该的BitmapFactory.Options类可以解决您的问题(http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html#inSampleSize).它的工作原理是制作一个inSampleSize比原始宽度和高度为1 /的位图,从而减少内存消耗(减少inSampleSize^ 2?).您应该在使用之前阅读该文档.

例:

BitmapFactory.Options options = new BitmapFactory.Options();
// will results in a much smaller image than the original
options.inSampleSize = 8;

// don't ever use a path to /sdcard like this, but I'm sure you have a sane way to do that
// in this case nebulae.jpg is a 19MB 8000x3874px image
final Bitmap b = BitmapFactory.decodeFile("/sdcard/nebulae.jpg", options);

final ImageView iv = (ImageView)findViewById(R.id.image_id);
iv.setImageBitmap(b);
Log.d("ExampleImage", "decoded bitmap dimensions:" + b.getWidth() + "x" + b.getHeight()); // 1000x485
Run Code Online (Sandbox Code Playgroud)

但是在这里它只适用于图像,我猜,inSampleSize是允许内存大小的^ 2倍,会降低小图像的质量.诀窍是找到合适的inSampleSize.