如何在 Android 中修复 java.lang.OutOfMemoryError

How to fix java.lang.OutOfMemoryError in Android

我想开发 android 应用程序,在这个应用程序中使用了很多方法。但有时在使用应用程序时会在 LogCat 中向我显示此错误:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: ir.mototel.mototel, PID: 4899
                  java.lang.OutOfMemoryError: Failed to allocate a 490012 byte allocation with 316664 free bytes and 309KB until OOM
                      at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
                      at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
                      at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)
                      at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
                      at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1080)
                      at android.content.res.Resources.loadDrawableForCookie(Resources.java:2635)
                      at android.content.res.Resources.loadDrawable(Resources.java:2540)
                      at android.content.res.TypedArray.getDrawable(TypedArray.java:870)
                      at android.widget.ImageView.<init>(ImageView.java:152)
                      at android.widget.ImageView.<init>(ImageView.java:140)
                      at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:60)
                      at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:56)
                      at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:106)
                      at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1017)
                      at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1076)
                      at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
                      at android.view.LayoutInflater$FactoryMerger.onCreateView(LayoutInflater.java:189)
                      at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:746)
                      at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
                      at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
                      at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
                      at android.view.LayoutInflater.rInflate(LayoutInflater.java:838)
                      at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
                      at android.view.LayoutInflater.rInflate(LayoutInflater.java:838)
                      at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
                      at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
                      at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
                      at com.sepandar.xengine.singleVideo.view.DetailFragment.onCreateView(DetailFragment.java:182)
                      at android.support.v4.app.Fragment.performCreateView(Fragment.java:2087)
                      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1113)
                      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1295)
                      at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801)
                      at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1643)
                      at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:679)
                      at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:143)
                      at android.support.v4.view.ViewPager.populate(ViewPager.java:1272)
                      at android.support.v4.view.ViewPager.populate(ViewPager.java:1120)
                      at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1646)
                      at android.view.View.measure(View.java:18788)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                      at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                      at android.view.View.measure(View.java:18788)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                      at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
                      at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
                      at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
                      at android.view.View.measure(View.java:18788)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                      at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:703)
                      at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:770)
                      at android.view.View.measure(View.java:18788)
                    at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.

我将这段代码写在 Application 标签中 Manifest android:largeHeap="true" 但是再次显示这个错误!

我该如何解决?请帮助我<3

在您的 res 文件夹中创建一个名为 drawable-nodpi 的文件夹。将您想要显示的所有图像(包括 placeholder 图像)放入该文件夹。之后像这样加载图像

Glide.with(context)
   .load(content.getThumbImage())
   .asBitmap()‌​.transform(new CenterCrop(context))
   .diskCacheStrategy( DiskCacheStrategy.NONE )
   .skipMemoryCache( true )
   .placeholder(R.drawable.placeholder)
   .int‌​o(thumbnail)

让我知道这是否适合你