Android 应用程序中的 OutOfMemoryError
Posted
技术标签:
【中文标题】Android 应用程序中的 OutOfMemoryError【英文标题】:OutOfMemoryError in Android App 【发布时间】:2013-04-17 14:24:58 【问题描述】:谁能向我解释一下这个 LogCat 输出以及如何解决它?我正在用我的手机(三星 Galaxy Nexus)测试我的 android 应用程序,就这样;系统强制扣留并打印在 LogCat 中...
04-17 09:43:09.189: E/AndroidRuntime(1194): FATAL EXCEPTION: main
04-17 09:43:09.189: E/AndroidRuntime(1194): java.lang.OutOfMemoryError
04-17 09:43:09.189: E/AndroidRuntime(1194): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
04-17 09:43:09.189: E/AndroidRuntime(1194): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:527)
04-17 09:43:09.189: E/AndroidRuntime(1194): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:301)
04-17 09:43:09.189: E/AndroidRuntime(1194): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:326)
04-17 09:43:09.189: E/AndroidRuntime(1194): at android.graphics.drawable.Drawable.createFromPath(Drawable.java:894)
04-17 09:43:09.189: E/AndroidRuntime(1194): at android.widget.ImageView.resolveUri(ImageView.java:638)
04-17 09:43:09.189: E/AndroidRuntime(1194): at android.widget.ImageView.setImageURI(ImageView.java:379)
04-17 09:43:09.189: E/AndroidRuntime(1194): at tian.proto.galeriaImagenes$ImageAdapter.getView(galeriaImagenes.java:169)
04-17 09:43:09.189: E/AndroidRuntime(1194): at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:193)
04-17 09:43:09.189: E/AndroidRuntime(1194): at android.view.View.measure(View.java:15172)
04-17 09:43:09.189: E/AndroidRuntime(1194): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
04-17 09:43:09.189: E/AndroidRuntime(1194): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
04-17 09:43:09.189: E/AndroidRuntime(1194): at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
04-17 09:43:09.189: E/AndroidRuntime(1194): at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
04-17 09:43:09.189: E/AndroidRuntime(1194): at android.view.View.measure(View.java:15172)
04-17 09:43:09.189: E/AndroidRuntime(1194): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
04-17 09:43:09.189: E/AndroidRuntime(1194): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
04-17 09:43:09.189: E/AndroidRuntime(1194): at android.view.View.measure(View.java:15172)
04-17 09:43:09.189: E/AndroidRuntime(1194): at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
04-17 09:43:09.189: E/AndroidRuntime(1194): at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
04-17 09:43:09.189: E/AndroidRuntime(1194): at android.view.View.measure(View.java:15172)
04-17 09:43:09.189: E/AndroidRuntime(1194): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
04-17 09:43:09.189: E/AndroidRuntime(1194): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
04-17 09:43:09.189: E/AndroidRuntime(1194): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2148)
04-17 09:43:09.189: E/AndroidRuntime(1194): at android.view.View.measure(View.java:15172)
04-17 09:43:09.189: E/AndroidRuntime(1194): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1850)
04-17 09:43:09.189: E/AndroidRuntime(1194): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1102)
04-17 09:43:09.189: E/AndroidRuntime(1194): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1275)
04-17 09:43:09.189: E/AndroidRuntime(1194): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
04-17 09:43:09.189: E/AndroidRuntime(1194): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4214)
04-17 09:43:09.189: E/AndroidRuntime(1194): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
04-17 09:43:09.189: E/AndroidRuntime(1194): at android.view.Choreographer.doCallbacks(Choreographer.java:555)
04-17 09:43:09.189: E/AndroidRuntime(1194): at android.view.Choreographer.doFrame(Choreographer.java:525)
04-17 09:43:09.189: E/AndroidRuntime(1194): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
04-17 09:43:09.189: E/AndroidRuntime(1194): at android.os.Handler.handleCallback(Handler.java:615)
04-17 09:43:09.189: E/AndroidRuntime(1194): at android.os.Handler.dispatchMessage(Handler.java:92)
04-17 09:43:09.189: E/AndroidRuntime(1194): at android.os.Looper.loop(Looper.java:137)
04-17 09:43:09.189: E/AndroidRuntime(1194): at android.app.ActivityThread.main(ActivityThread.java:4899)
04-17 09:43:09.189: E/AndroidRuntime(1194): at java.lang.reflect.Method.invokeNative(Native Method)
04-17 09:43:09.189: E/AndroidRuntime(1194): at java.lang.reflect.Method.invoke(Method.java:511)
04-17 09:43:09.189: E/AndroidRuntime(1194): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
04-17 09:43:09.189: E/AndroidRuntime(1194): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
04-17 09:43:09.189: E/AndroidRuntime(1194): at dalvik.system.NativeStart.main(Native Method)
【问题讨论】:
【参考方案1】:如果您在运行时加载位图,您可以在使用this technique 从设备读取它时将其缩小。如果您从res
目录加载它,您可能只需要使用图像编辑器将其缩小。无论哪种方式,图像显然都太大而无法放入内存。
【讨论】:
【参考方案2】:如我所见,您使用ImageView.setImageURI
加载图像。
现在许多图像,尤其是由更高分辨率的相机(或手机)拍摄的图像,很容易占用比在屏幕上显示它们实际需要的空间更多的空间。
当您尝试将它们直接加载到位图中时,您的应用将耗尽内存并崩溃。 另请参阅此question 或@oakleaf 的答案,以获取有关使用大型位图的 Android 开发人员培训链接。
【讨论】:
以上是关于Android 应用程序中的 OutOfMemoryError的主要内容,如果未能解决你的问题,请参考以下文章
内存溢出系列OutOfMemoryError:Java heap space(1/8)