我的应用程序的主要目的是以图像中显示的方式显示图像,如下所示:
private void setSelectedImage(int selectedImagePosition)
{
BitmapDrawable bd = (BitmapDrawable) drawables.get(selectedImagePosition);
Bitmap b = Bitmap.createScaledBitmap(bd.getBitmap(), (int) (bd.getIntrinsicHeight() * 0.9), (int) (bd.getIntrinsicWidth() * 0.7), false);
selectedImageView.setImageBitmap(b);
selectedImageView.setScaleType(ScaleType.FIT_XY);
}
详细代码可以在这里找到。
异常发生在以下行:
Bitmap b = Bitmap.createScaledBitmap(bd.getBitmap(), (int) (bd.getIntrinsicHeight() * 0.9), (int) (bd.getIntrinsicWidth() * 0.7), false);
上面的函数被从onItemSelected中调用。该应用在2.2和2.3上仍能正常工作,但在4.1上立即抛出异常。上述代码运行良好,但会抛出以下异常。我没有看到2.2和2.3中的任何崩溃,但它在4.1中立即崩溃。在Jelly Bean中是否存在主要的内存管理差异?
java.lang.OutOfMemoryError
AndroidRuntime(2616): at android.graphics.Bitmap.nativeCreate(Native Method)
AndroidRuntime(2616): at android.graphics.Bitmap.createBitmap(Bitmap.java:640)
AndroidRuntime(2616): at android.graphics.Bitmap.createBitmap(Bitmap.java:586)
AndroidRuntime(2616): at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:466)
AndroidRuntime(2616): at com.rdx.gallery.GalleryDemoActivity.setSelectedImage(GalleryDemoActivity.java:183)