微小的图像,没有旋转,但仍获得的OutOfMemoryError:位图大小超过VM预算 [英] Tiny Images, No Rotation, but still get OutOfMemoryError: bitmap size exceeds VM budget

查看:129
本文介绍了微小的图像,没有旋转,但仍获得的OutOfMemoryError:位图大小超过VM预算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了大约早上这个确切的错误计算器帖子页面后,通过页面读书,但他们的问题的根源似乎总是要么1)他们需要被下采样非常大的图像或2)他们经过一个或多个设备的旋转造成的活性被破坏并重新创建几次痛苦。

I've spent the morning reading through page after page of StackOverflow posts about this exact error, but the root cause of their problems always seems to be either 1) they have very large images which need to be down sampled or 2) they are suffering after one or more device rotations which cause the activity to be destroyed and recreated several times.

我得到这个错误(只有很少),但我的应用程序只允许纵向所以没有旋转是可能的,我所有的图片都非常小。我必须的许多的图像,但他们都非常小。这是一个计算器应用程序,所以有每个按钮的图像。该图像只1.9K为MDPI和2.8K的华电国际。这些是很小,很小的图像。我处理他们通过Photoshop来下采样他们都8bit的PNG图像。这是我的图像中的一个的示例: http://geekqi.com/StackOverflow/OutOfMemoryImage.png

I'm getting this error (only very rarely), but my app only allows portrait orientation so no rotations are possible and all of my images are very small. I do have many images, but they are all very small. It is a calculator app, so there are images for each button. The images are only 1.9k for the mdpi and 2.8k for the hdpi. These are tiny, tiny images. I processed them through Photoshop to downsample all of them to 8bit PNGs. Here is an example of one of my images: http://geekqi.com/StackOverflow/OutOfMemoryImage.png

有这些ImageButtons约80(其中只有40是在屏幕上一次)。我有两个观点,各有40 ImageButtons,当你preSS一个按钮,其中一个视图是隐藏的,另一个显示(提供移功能,让使用更多功能)。每个按钮都有一个标准的图像和突出显示的图像。所以,这一切的一切我处理160微小的图像。最大的一个是2.8K所以他们的160是448K ......即使ImageButton的开销将是规模翻番,这是小于1MB。

There are about 80 of these ImageButtons (only 40 of which are on the screen at once). I have two views, each with 40 ImageButtons and when you press a button, one of the views is hidden and the other shown (offering a Shift functionality to allow access to more functions). Each button has a standard image and a highlighted image. So, all in all I'm dealing with 160 tiny images. The biggest one is 2.8k so 160 of them would be 448k... even if the ImageButton overhead would double that size, it's less than 1MB.

下面是我收到的堆栈跟踪。它引用线296,它看起来像:

Below is the stacktrace I'm receiving. It references line 296, which looks like:

<ImageButton android:id="@+id/buttonRND" android:src="@drawable/image_rnd" android:hapticFeedbackEnabled="true" style="@style/CalculatorButton"/>

下面是我处理堆栈跟踪:

Here is the stack trace I'm dealing with:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mydomain.myapp/com.mydomain.myapp.CalculatorTabActivity}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mydomain.myapp/com.mydomain.myapp.MyCalculator}: android.view.InflateException: Binary XML file line #296: Error inflating class <unknown>
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
       at android.app.ActivityThread.access$2300(ActivityThread.java:125)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
       at android.os.Handler.dispatchMessage(Handler.java:99)
       at android.os.Looper.loop(Looper.java:123)
       at android.app.ActivityThread.main(ActivityThread.java:4627)
       at java.lang.reflect.Method.invokeNative(Native Method)
       at java.lang.reflect.Method.invoke(Method.java:521)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:871)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)
       at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mydomain.myapp/com.mydomain.myapp.MyCalculator}: android.view.InflateException: Binary XML file line #296: Error inflating class <unknown>
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
       at android.app.ActivityThread.startActivityNow(ActivityThread.java:2503)
       at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
       at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
       at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:651)
       at android.widget.TabHost.setCurrentTab(TabHost.java:323)
       at android.widget.TabHost.addTab(TabHost.java:213)
       at com.mydomain.myapp.CalculatorTabActivity.setupTab(CalculatorTabActivity.java:191)
       at com.mydomain.myapp.CalculatorTabActivity.onCreate(CalculatorTabActivity.java:95)
       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
       ... 11 more
Caused by: android.view.InflateException: Binary XML file line #296: Error inflating class <unknown>
       at android.view.LayoutInflater.createView(LayoutInflater.java:513)
       at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
       at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
       at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
       at android.view.LayoutInflater.rInflate(LayoutInflater.java:621)
       at android.view.LayoutInflater.rInflate(LayoutInflater.java:621)
       at android.view.LayoutInflater.rInflate(LayoutInflater.java:621)
       at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
       at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
       at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
       at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:210)
       at android.app.Activity.setContentView(Activity.java:1647)
       at com.mydomain.myapp.MyCalculator.configureActivity(MyCalculator.java:1042)
       at com.mydomain.myapp.MyCalculator.onCreate(MyCalculator.java:1111)
       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
       ... 21 more
Caused by: java.lang.reflect.InvocationTargetException
       at android.widget.ImageButton.<init>(ImageButton.java:78)
       at java.lang.reflect.Constructor.constructNative(Native Method)
       at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
       at android.view.LayoutInflater.createView(LayoutInflater.java:500)
       ... 36 more
Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
       at android.graphics.Bitmap.nativeCreate(Native Method)
       at android.graphics.Bitmap.createBitmap(Bitmap.java:468)
       at android.graphics.Bitmap.createBitmap(Bitmap.java:435)
       at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:340)
       at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:590)
       at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:564)
       at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:425)
       at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
       at android.content.res.Resources.loadDrawable(Resources.java:1709)
       at android.content.res.Resources.getDrawable(Resources.java:581)
       at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:162)
       at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:787)
       at android.graphics.drawable.Drawable.createFromXml(Drawable.java:728)
       at android.content.res.Resources.loadDrawable(Resources.java:1694)
       at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
       at android.widget.ImageView.<init>(ImageView.java:118)
       at android.widget.ImageButton.<init>(ImageButton.java:82)
       ... 40 more

我已经通过所有关于跟踪Android的内存泄漏的链接阅读,但他们也处理旋转和我的应用程序不允许转动。

I've read through all of the links about tracking down Android memory "leaks", but they also deal with rotation and my app doesn't allow rotation.

我能做些什么来查找和解决这个问题?

What can I do to find and fix this problem?

推荐答案

您不必旋转应用获取活动重新开始。这足以启动另一个活动,然后启动原来的。

You don't need to rotate app to get Activity restarted. It is enough to start another activity and then start the original one.

你保持你的活动里面的图像的引用?你如何保持呢?在字段?你引用的图像绘制资源?

Do you keep references to your images inside your activity? How do you keep them? In fields? Are you referencing image drawables?

这篇关于微小的图像,没有旋转,但仍获得的OutOfMemoryError:位图大小超过VM预算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆