如何在加载活动时解决java.lang.OutOfMemoryError

时间:2017-05-08 05:35:23

标签: java android out-of-memory android-view image-loading

每次加载名为ProductActivity的某些活动时,都会遇到以下错误:

Fatal Exception: java.lang.OutOfMemoryError: Failed to allocate a 15694612 byte allocation with 2874400 free bytes and 2MB until OOM
       at dalvik.system.VMRuntime.newNonMovableArray(VMRuntime.java)
       at android.graphics.BitmapFactory.nativeDecodeAsset(BitmapFactory.java)
       at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:613)
       at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:446)
       at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:988)
       at android.content.res.Resources.createFromResourceStream(Resources.java:2813)
       at android.content.res.Resources.loadDrawableForCookie(Resources.java:2514)
       at android.content.res.Resources.loadDrawable(Resources.java:2416)
       at android.content.res.MiuiResources.loadDrawable(MiuiResources.java:393)
       at android.content.res.TypedArray.getDrawable(TypedArray.java:751)
       at android.view.View.<init>(View.java:3740)
       at android.widget.ImageView.<init>(ImageView.java:139)
       at android.widget.ImageView.<init>(ImageView.java:135)
       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:47)
       at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:729)
       at android.view.LayoutInflater.rInflate(LayoutInflater.java:810)
       at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
       at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
       at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
       at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
       at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
       at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
       at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
       at android.view.LayoutInflater.inflate(LayoutInflater.java:508)
       at android.view.LayoutInflater.inflate(LayoutInflater.java:418)
       at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
       at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284)
       at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:143)
       at demand.inn.com.quflip.activity.ProductInfoActivity.onCreate(ProductInfoActivity.java:73)
       at android.app.Activity.performCreate(Activity.java:6041)
       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1109)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2283)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2392)
       at android.app.ActivityThread.access$800(ActivityThread.java:154)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:135)
       at android.app.ActivityThread.main(ActivityThread.java:5273)
       at java.lang.reflect.Method.invoke(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:372)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)

现在我应该如何优化资源以便摆脱这个错误?

我应该调整服务器上的图像大小,还是应该在我的应用程序中调整它们的大小以免除所有这些。 目前,我正在使用毕加索来加载我的应用程序中的图像。

1 个答案:

答案 0 :(得分:2)

您正在尝试加载15694612字节= 15 MB的称重图像,这太大了。如果没有选项可以使用单独的缩减采样资源,那么您的选项是downsample it programmatically,然后加载它。

private Bitmap downscaleBitmapUsingDensities(final int sampleSize, final int imageResId) {
  final Options bitmapOptions = new Options();
  bitmapOptions.inDensity = sampleSize;
  bitmapOptions.inTargetDensity = 1;
  final Bitmap scaledBitmap = BitmapFactory.decodeResource(getResources(), imageResId, bitmapOptions);
  scaledBitmap.setDensity(Bitmap.DENSITY_NONE);
  return scaledBitmap;
}   

sampleSize是一个整数,它指定在将源位图加载到内存之前对源位图进行缩减采样的次数。