我有一个RecyclerView,其中包含许多小图像(200x200,8kb)。 在我的开发者控制台中,我从具有Android 7和7.1版的设备上收到许多OOM崩溃报告。
这是OutOfMemory崩溃:
java.lang.OutOfMemoryError:
at dalvik.system.VMRuntime.newNonMovableArray (Native Method)
at android.graphics.BitmapFactory.nativeDecodeAsset (Native Method)
at android.graphics.BitmapFactory.decodeStream (BitmapFactory.java:700)
at android.graphics.BitmapFactory.decodeResourceStream (BitmapFactory.java:535)
at android.graphics.drawable.Drawable.createFromResourceStream (Drawable.java:1179)
at android.content.res.ResourcesImpl.loadDrawableForCookie (ResourcesImpl.java:770)
at android.content.res.ResourcesImpl.loadDrawable (ResourcesImpl.java:621)
at android.content.res.Resources.getDrawable (Resources.java:1640)
at android.content.Context.getDrawable (Context.java:525)
at android.support.v4.content.ContextCompat.getDrawable (ContextCompat.java:463)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable (AppCompatDrawableManager.java:203)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable (AppCompatDrawableManager.java:191)
at android.support.v7.content.res.AppCompatResources.getDrawable (AppCompatResources.java:102)
at android.support.v7.widget.AppCompatImageHelper.setImageResource (AppCompatImageHelper.java:86)
at android.support.v7.widget.AppCompatImageView.setImageResource (AppCompatImageView.java:94)
at com.censored.censored.Tab1Adapter.onBindViewHolder (Tab1Adapter.java:135)
at com.censored.censored.Tab1Adapter.onBindViewHolder (Tab1Adapter.java:19)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder (RecyclerView.java:6781)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder (RecyclerView.java:6823)
at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline (RecyclerView.java:5752)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline (RecyclerView.java:6019)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition (RecyclerView.java:5858)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition (RecyclerView.java:5854)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next (LinearLayoutManager.java:2230)
at android.support.v7.widget.GridLayoutManager.layoutChunk (GridLayoutManager.java:557)
at android.support.v7.widget.LinearLayoutManager.fill (LinearLayoutManager.java:1517)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren (LinearLayoutManager.java:612)
at android.support.v7.widget.GridLayoutManager.onLayoutChildren (GridLayoutManager.java:171)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2 (RecyclerView.java:3924)
at android.support.v7.widget.RecyclerView.dispatchLayout (RecyclerView.java:3641)
at android.support.v7.widget.RecyclerView.onLayout (RecyclerView.java:4194)
at android.view.View.layout (View.java:18798)
at android.view.ViewGroup.layout (ViewGroup.java:5951)
at android.widget.RelativeLayout.onLayout (RelativeLayout.java:1079)
at android.view.View.layout (View.java:18798)
at android.view.ViewGroup.layout (ViewGroup.java:5951)
at android.support.v4.view.ViewPager.onLayout (ViewPager.java:1775)
at android.view.View.layout (View.java:18798)
at android.view.ViewGroup.layout (ViewGroup.java:5951)
at android.widget.LinearLayout.setChildFrame (LinearLayout.java:1741)
at android.widget.LinearLayout.layoutVertical (LinearLayout.java:1585)
at android.widget.LinearLayout.onLayout (LinearLayout.java:1494)
at android.view.View.layout (View.java:18798)
at android.view.ViewGroup.layout (ViewGroup.java:5951)
at android.widget.FrameLayout.layoutChildren (FrameLayout.java:323)
at android.widget.FrameLayout.onLayout (FrameLayout.java:261)
at android.view.View.layout (View.java:18798)
at android.view.ViewGroup.layout (ViewGroup.java:5951)
at android.support.v4.widget.DrawerLayout.onLayout (DrawerLayout.java:1231)
at android.view.View.layout (View.java:18798)
at android.view.ViewGroup.layout (ViewGroup.java:5951)
at android.widget.LinearLayout.setChildFrame (LinearLayout.java:1741)
at android.widget.LinearLayout.layoutVertical (LinearLayout.java:1585)
at android.widget.LinearLayout.onLayout (LinearLayout.java:1494)
at android.view.View.layout (View.java:18798)
at android.view.ViewGroup.layout (ViewGroup.java:5951)
at android.widget.FrameLayout.layoutChildren (FrameLayout.java:323)
at android.widget.FrameLayout.onLayout (FrameLayout.java:261)
at android.view.View.layout (View.java:18798)
at android.view.ViewGroup.layout (ViewGroup.java:5951)
at android.widget.LinearLayout.setChildFrame (LinearLayout.java:1741)
at android.widget.LinearLayout.layoutVertical (LinearLayout.java:1585)
at android.widget.LinearLayout.onLayout (LinearLayout.java:1494)
at android.view.View.layout (View.java:18798)
at android.view.ViewGroup.layout (ViewGroup.java:5951)
at android.widget.FrameLayout.layoutChildren (FrameLayout.java:323)
at android.widget.FrameLayout.onLayout (FrameLayout.java:261)
at android.view.View.layout (View.java:18798)
at android.view.ViewGroup.layout (ViewGroup.java:5951)
at android.widget.LinearLayout.setChildFrame (LinearLayout.java:1741)
at android.widget.LinearLayout.layoutVertical (LinearLayout.java:1585)
at android.widget.LinearLayout.onLayout (LinearLayout.java:1494)
at android.view.View.layout (View.java:18798)
at android.view.ViewGroup.layout (ViewGroup.java:5951)
at android.widget.FrameLayout.layoutChildren (FrameLayout.java:323)
at android.widget.FrameLayout.onLayout (FrameLayout.java:261)
at com.android.internal.policy.DecorView.onLayout (DecorView.java:887)
at android.view.View.layout (View.java:18798)
at android.view.ViewGroup.layout (ViewGroup.java:5951)
at android.view.ViewRootImpl.performLayout (ViewRootImpl.java:2648)
at android.view.ViewRootImpl.performTraversals (ViewRootImpl.java:2364)
at android.view.ViewRootImpl.doTraversal (ViewRootImpl.java:1515)
at android.view.ViewRootImpl$TraversalRunnable.run (ViewRootImpl.java:7091)
at android.view.Choreographer$CallbackRecord.run (Choreographer.java:927)
at android.view.Choreographer.doCallbacks (Choreographer.java:702)
at android.view.Choreographer.doFrame (Choreographer.java:638)
at android.view.Choreographer$FrameDisplayEventReceiver.run (Choreographer.java:913)
at android.os.Handler.handleCallback (Handler.java:751)
at android.os.Handler.dispatchMessage (Handler.java:95)
at android.os.Looper.loop (Looper.java:154)
at android.app.ActivityThread.main (ActivityThread.java:6682)
at java.lang.reflect.Method.invoke (Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1410)
这是导致崩溃的代码:
holder1.itemImageView.setImageResource(imageResources[position]);
holder1.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {
final Context context = v.getContext();
holder1.itemImageView.setImageResource(pressedResource[position]);
new Handler().postDelayed(new Runnable() {
public void run() {
holder1.itemImageView.setImageResource(imageResources[position]);
}
}, 500);
// Calls a method that plays the sound
// Should be handled in an extra thread
new Thread(new Runnable() {
@Override
public void run() {
EventHandlerClass.startMediaPlayer(v, soundID);
}
}).start();
}
});
我可以抓住OOM崩溃并告诉它使用较小的图像大小吗? 如果是,该怎么办?
我已经尝试使用图像加载库Picasso来防止OOM崩溃,但是这导致图像加载时间太长,因此看起来如下所示: https://media.giphy.com/media/STlGbpXvT8B9iBvnQS/giphy.gif
还是有其他方法可以解决OOM崩溃?
我真的需要帮助,由于坠机原因,我收到了很多不好的评价。
谢谢:)