参见英文答案 >
Strange out of memory issue while loading an image to a Bitmap object41个
我想在我的Android应用程序中实现启动页面.所以在布局xml中,有一个imageView,而图像文件是一个1080 * 1920的png文件,当运行应用程序时,它会发生OutOfMemoryError错误,我该如何解决?
我想在我的Android应用程序中实现启动页面.所以在布局xml中,有一个imageView,而图像文件是一个1080 * 1920的png文件,当运行应用程序时,它会发生OutOfMemoryError错误,我该如何解决?
XML文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/welcome1"/> </LinearLayout>
错误信息:
04-17 18:40:16.038 29793-29793/cn.test.android E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: cn.test.android,PID: 29793 java.lang.OutOfMemoryError at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:597) at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:432) at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:840) at android.content.res.Resources.createFromResourceStream(Resources.java:2477) at android.content.res.Resources.loadDrawable(Resources.java:2136) at android.content.res.MiuiResources.loadDrawable(MiuiResources.java:320) at android.content.res.Resources.getDrawable(Resources.java:710) at android.widget.ImageView.resolveUri(ImageView.java:638) at android.widget.ImageView.setImageResource(ImageView.java:367) at cn.test.android.LandingPageActivity$1.instantiateItem(LandingPageActivity.java:40) at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:837) at android.support.v4.view.ViewPager.populate(ViewPager.java:987) at android.support.v4.view.ViewPager.populate(ViewPager.java:919) at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1441) at android.view.View.measure(View.java:16508) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125) at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404) at android.widget.LinearLayout.measureVertical(LinearLayout.java:695) at android.widget.LinearLayout.onMeasure(LinearLayout.java:588) at android.view.View.measure(View.java:16508) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125) at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) at android.view.View.measure(View.java:16508) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125) at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404) at android.widget.LinearLayout.measureVertical(LinearLayout.java:695) at android.widget.LinearLayout.onMeasure(LinearLayout.java:588) at android.view.View.measure(View.java:16508) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125) at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291) at android.view.View.measure(View.java:16508) at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1946) at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1143) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1325) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1030) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5639) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761) at android.view.Choreographer.doCallbacks(Choreographer.java:574) at android.view.Choreographer.doFrame(Choreographer.java:544) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5047) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:806) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622) at dalvik.system.NativeStart.main(Native Method)
解决方法
如果您在drawable文件夹中有welcome1.png,请将其放在drawable-nodpi中.
这是因为drawable被解释为drawable-mdpi,因此当你将它加载到更高密度的手机上时,它将被放大(成为一个巨大的位图).