OutOfMemoryError: Using a image which is 1080*1920 do the splash page
本问题已经有最佳答案,请猛点这里访问。
我想在我的Android应用程序中实现启动页面。 所以在布局xml中,有一个imageView,而图像文件是一个
XML文件:
1 2 3 4 5 6 7 8 9 10 11 | <?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> |
错误信息:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | 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) |
如果
这是因为
尝试缩小原始图像(保持纵横比),然后在
1 2 3 4 5 | <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="fitCenter" android:background="@drawable/welcome1"/> |
Android一般都有图像问题,特别是大图像。这是由于允许App分配的最大金额。您可以在加载位图时重新取样位图。
作为一个简短的例子,这将把图像的大小减少一半:
1 2 3 | final BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inSampleSize = 2; Bitmap bm = BitmapFactory.decodeStream(is, null, opts); |
分配给每个应用程序的内存在Android中是稀疏的,您不应该像这样将它提供给图像。
您的应用程序因内存不足而导致此错误。
1 | what you can do is increase cahce memory of emulator to support your memory usage. |
好吧,这不是确切的解决方案,尝试最小化图像分辨率,你可以摆脱这个错误,