解决方法
编辑:这个问题实际上已经在
Strange out of memory issue while loading an image to a Bitmap object回答了(两个最高的投票答案).它也使用inSampleSize选项,但使用一个小方法来自动获取适当的值.
我原来的回答是:
BitmapFactory.Options类的inSampleSize可以解决您的问题(http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html#inSampleSize).它的工作原理是制作一个比原始宽度和高度为1 / inSampleSize的位图,从而减少内存消耗(通过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@H_502_17@然而,在这里它只适用于图像,我猜,在SampleSize ^ 2倍于允许的内存大小,并将降低小图像的质量.诀窍是找到合适的inSampleSize.