我一直收到java.lang.outofmemory错误

时间:2013-08-08 18:54:27

标签: android image android-layout

我正在尝试在我的应用上设置一个背景,可以在平板电脑和手机上很好地扩展。所以我得到了一个png背景,尺寸为2560×1600,但尺寸只有44k。但是,每次我运行应用程序时,我都会继续这样做。

08-08 14:50:21.251: E/AndroidRuntime(2924): FATAL EXCEPTION: main
08-08 14:50:21.251: E/AndroidRuntime(2924): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.reflap.reflap/com.reflap.reflap.MainActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class <unknown>
08-08 14:50:21.251: E/AndroidRuntime(2924):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
08-08 14:50:21.251: E/AndroidRuntime(2924):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
08-08 14:50:21.251: E/AndroidRuntime(2924):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-08 14:50:21.251: E/AndroidRuntime(2924):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
08-08 14:50:21.251: E/AndroidRuntime(2924):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-08 14:50:21.251: E/AndroidRuntime(2924):     at android.os.Looper.loop(Looper.java:137)
08-08 14:50:21.251: E/AndroidRuntime(2924):     at android.app.ActivityThread.main(ActivityThread.java:5103)
08-08 14:50:21.251: E/AndroidRuntime(2924):     at java.lang.reflect.Method.invokeNative(Native Method)
08-08 14:50:21.251: E/AndroidRuntime(2924):     at java.lang.reflect.Method.invoke(Method.java:525)
08-08 14:50:21.251: E/AndroidRuntime(2924):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-08 14:50:21.251: E/AndroidRuntime(2924):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-08 14:50:21.251: E/AndroidRuntime(2924):     at dalvik.system.NativeStart.main(Native Method)
08-08 14:50:21.251: E/AndroidRuntime(2924): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class <unknown>
08-08 14:50:21.251: E/AndroidRuntime(2924):     at android.view.LayoutInflater.createView(LayoutInflater.java:620)
08-08 14:50:21.251: E/AndroidRuntime(2924):     at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
08-08 14:50:21.251: E/AndroidRuntime(2924):     at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
08-08 14:50:21.251: E/AndroidRuntime(2924):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
08-08 14:50:21.251: E/AndroidRuntime(2924):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
08-08 14:50:21.251: E/AndroidRuntime(2924):     at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
08-08 14:50:21.251: E/AndroidRuntime(2924):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
08-08 14:50:21.251: E/AndroidRuntime(2924):     at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
08-08 14:50:21.251: E/AndroidRuntime(2924):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:267)
08-08 14:50:21.251: E/AndroidRuntime(2924):     at android.app.Activity.setContentView(Activity.java:1895)
08-08 14:50:21.251: E/AndroidRuntime(2924):     at com.reflap.reflap.MainActivity.onCreate(MainActivity.java:18)
08-08 14:50:21.251: E/AndroidRuntime(2924):     at android.app.Activity.performCreate(Activity.java:5133)
08-08 14:50:21.251: E/AndroidRuntime(2924):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-08 14:50:21.251: E/AndroidRuntime(2924):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
08-08 14:50:21.251: E/AndroidRuntime(2924):     ... 11 more
08-08 14:50:21.251: E/AndroidRuntime(2924): Caused by: java.lang.reflect.InvocationTargetException
08-08 14:50:21.251: E/AndroidRuntime(2924):     at java.lang.reflect.Constructor.constructNative(Native Method)
08-08 14:50:21.251: E/AndroidRuntime(2924):     at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
08-08 14:50:21.251: E/AndroidRuntime(2924):     at android.view.LayoutInflater.createView(LayoutInflater.java:594)
08-08 14:50:21.251: E/AndroidRuntime(2924):     ... 24 more
08-08 14:50:21.251: E/AndroidRuntime(2924): Caused by: java.lang.OutOfMemoryError
08-08 14:50:21.251: E/AndroidRuntime(2924):     at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
08-08 14:50:21.251: E/AndroidRuntime(2924):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:503)
08-08 14:50:21.251: E/AndroidRuntime(2924):     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:356)
08-08 14:50:21.251: E/AndroidRuntime(2924):     at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:800)
08-08 14:50:21.251: E/AndroidRuntime(2924):     at android.content.res.Resources.loadDrawable(Resources.java:2105)
08-08 14:50:21.251: E/AndroidRuntime(2924):     at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
08-08 14:50:21.251: E/AndroidRuntime(2924):     at android.widget.ImageView.<init>(ImageView.java:127)
08-08 14:50:21.251: E/AndroidRuntime(2924):     at android.widget.ImageView.<init>(ImageView.java:117)
08-08 14:50:21.251: E/AndroidRuntime(2924):     ... 27 more

我不明白如此小的尺寸会如何耗尽内存。

这是我对imageview的布局xml

<ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:scaleType="center"
        android:src="@drawable/back" />

3 个答案:

答案 0 :(得分:1)

在内部,您的.png用每像素一个整数表示(默认情况下)。整数是4个字节。如果你做数学运算:

2560 pixels * 1600 pixels * 4 bytes / pixel = 16,384,000 bytes = 15.625 MB

旧Android设备上每个应用的内存限制为16 MB。便携式网络图形格式通常是压缩的,这就是为什么文件大小和大小明显不同以表示内存中的图像的原因。此外,即使您的图像没有或需要Alpha通道,Android也会在Alpha通道上浪费一个字节。

答案 1 :(得分:1)

44k是压缩大小。要将其用作背景,它将解压缩为位图。位图的内存大小是每个像素的颜色格式的大小乘以宽度乘以高度。更糟糕的是,如果它被缩放,系统也需要更多的内存来进行缩放。

因此,例如,如果您使用此颜色格式: http://developer.android.com/reference/android/graphics/Bitmap.Config.html#RGB_565

然后,位图所需的内存为2 x 2560 x 1600 = 7.8125 MB。

有一个显示选项可以获得更多内存: http://developer.android.com/guide/topics/manifest/application-element.html#largeHeap

虽然您还应该确保为较低DPI设备提供较小的资源,并且只读取所需数据。如果您不需要每个像素,BitmapFactory类允许您在读取数据时跳过像素。 BitmapRegionDecoder让您只需读取所需图像的各个部分。

答案 2 :(得分:1)

避免使用位图填充背景。他们不会给出最好的结果,并且会在不保留屏幕比例的情况下进行缩放。

使用9个贴片,形状绘图,简单的颜色和/或固定在某个角落的小位图。