java.lang.OutOfMemoryError用4张图片开发应用程序

时间:2018-06-09 15:12:33

标签: java android out-of-memory

所以我正在尝试学习Android开发,我试图在ConstraintLayout中开发“Netflix App”,由于某些原因我的应用程序崩溃并且我得到了所有这些错误,我检查了照片的大小,它们都少了那么1mb

希望你们能解释一下并解决我的错误。

xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"
tools:context=".MainActivity">

<android.support.constraint.Guideline
    android:id="@+id/guideline3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    app:layout_constraintGuide_percent="0.18518518" />

<ImageView
    android:id="@+id/imageView"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginTop="8dp"
    android:scaleType="fitCenter"
    app:layout_constraintBottom_toTopOf="@+id/guideline3"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.428"
    app:srcCompat="@drawable/netflixlogo" />

<android.support.constraint.Guideline
    android:id="@+id/guideline5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintGuide_percent="0.90" />

<ImageView
    android:id="@+id/imageView24"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginLeft="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    app:layout_constraintBottom_toTopOf="@+id/imageView4"
    app:layout_constraintEnd_toStartOf="@+id/imageView3"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/imageView"
    app:srcCompat="@drawable/movie4" />

<ImageView
    android:id="@+id/imageView3"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginTop="8dp"
    app:layout_constraintBottom_toTopOf="@+id/imageView5"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toEndOf="@+id/imageView24"
    app:layout_constraintTop_toBottomOf="@+id/imageView"
    app:srcCompat="@drawable/movie11" />

<ImageView
    android:id="@+id/imageView4"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginBottom="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginStart="8dp"
    app:layout_constraintBottom_toTopOf="@+id/guideline5"
    app:layout_constraintEnd_toStartOf="@+id/imageView5"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/imageView24"
    app:srcCompat="@drawable/movie2" />

<ImageView
    android:id="@+id/imageView5"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    app:layout_constraintBottom_toTopOf="@+id/guideline5"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toEndOf="@+id/imageView4"
    app:layout_constraintTop_toBottomOf="@+id/imageView3"
    app:srcCompat="@drawable/movie3" />

这些是错误:

   java.lang.OutOfMemoryError: Failed to allocate a 177561612 byte allocation with 33542504 free bytes and 93MB until OOM
    at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
    at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:620)
    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:455)
    at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1152)
    at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:855)
    at android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:706)
    at android.content.res.Resources.getDrawable(Resources.java:776)
    at android.content.Context.getDrawable(Context.java:525)
    at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:358)
    at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:198)
    at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:186)
    at android.support.v7.content.res.AppCompatResources.getDrawable(AppCompatResources.java:100)
    at android.support.v7.widget.AppCompatImageHelper.loadFromAttributes(AppCompatImageHelper.java:58)
    at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:77)
    at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:67)
    at android.support.v7.app.AppCompatViewInflater.createImageView(AppCompatViewInflater.java:181)
    at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:105)
    at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1035)
    at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1092)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:769)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
    at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
    at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
    at com.example.nivsaparov.mynetflixapp.MainActivity.onCreate(MainActivity.java:12)
    at android.app.Activity.performCreate(Activity.java:6754)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2679)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2787)
    at android.app.ActivityThread.-wrap12(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1504)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6247)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)

2 个答案:

答案 0 :(得分:0)

禁用高级Android分析器为我做了诀窍。您可以按如下方式禁用它:

转到运行/调试配置 - &gt;分析 - &gt;取消选中启用高级分析

disable

答案 1 :(得分:0)

您正在加载的图像太大,占用太多内存来加载它们。 你可以尝试https://developer.android.com/topic/performance/graphics/load-bitmap 我建议使用库来加载图像,如Glide或Picasso。