OutOfMemoryError:使用1080 * 1920的图像执行启动页面

时间:2015-04-17 10:50:17

标签: android imageview out-of-memory

我想在我的Android应用中实现启动页面。所以在布局xml中,有一个imageView,图像文件是一个png文件,1080*1920,当运行应用程序时,它出现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)

4 个答案:

答案 0 :(得分:9)

如果welcome1.png文件夹中有drawable,请将其放在drawable-nodpi

这是因为drawable被解释为drawable-mdpi,因此当您将其加载到更高密度的手机上时,它将被放大(变成一个巨大的位图)。

答案 1 :(得分:1)

分配给每个应用程序的内存在Android中是稀疏的,你不应该像这样将它提供给图像。

您的应用程序内存不足,导致出现此错误。

what you can do is increase cahce memory of emulator to support your memory usage. 

这不是确切的解决方案,尝试最小化图像分辨率,你可以摆脱这个错误,

答案 2 :(得分:1)

Android一般都有图像问题,特别是大图像。这是由于允许App分配的最大金额。您可以在加载位图时对其重新取样。

作为一个简短的例子,这会将图像的大小减半:

final BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize = 2;
Bitmap bm = BitmapFactory.decodeStream(is, null, opts);

答案 3 :(得分:1)

尝试缩小原始图片(保持宽高比),然后在scaleType ro fitCenter centerCrop 中设置ImageView

<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scaleType="fitCenter" 
    android:background="@drawable/welcome1"/>