【问题标题】:Android Out of Memory error on drawable folder [duplicate]可绘制文件夹上的Android内存不足错误[重复]
【发布时间】:2015-11-18 20:55:04
【问题描述】:

在我的 android 应用程序中,将所有图像都放在 drawable 文件夹中。在大多数手机上,我们没有问题。但是有些手机有内存不足的错误。例如,当图像复制到 drawable-xhdpi 文件夹时,问题就消失了。这个问题是什么原因,我该如何解决?

【问题讨论】:

  • 图片太大了。
  • 这取决于设备的分辨率。您不能将 drawable-xhdpi 应用于分辨率较低的设备。这就是为什么还有其他文件夹
  • 尝试使用毕加索库
  • 图片尺寸不大

标签: android out-of-memory runtime-error


【解决方案1】:

drawable 等价于drawable-mdpi

如果您将图像放在该文件夹中,它们将针对更高分辨率的设备进行上采样,并且如果图像很大,上采样可能会触发 OOM。

如果您在 drawable-xhdpi 中放入相同大小的图像,您将仅在较大的 xxhdpi 设备上进行上采样,而在其他设备上进行下采样。

如果您想避免对图像进行自动上/下采样,请将它们放在drawable-nodpi 文件夹中。

【讨论】:

  • 谢谢你的回答。我尝试了 drawable-nodpi 文件夹并工作了。几次之后,更大屏幕尺寸的手机没有显示图像消失了。并将图像复制到 drawable-xhdpi 文件夹,然后再次正常工作。为什么会这样?
  • 我从来没有遇到过 Android 在 drawable-nodpi 文件夹中找不到资源的问题。 Android 将始终使用最合适文件夹中的图像,但如果在任何 dpi 特定文件夹中找不到它,它将使用nodpi。要了解有关它如何选择资源的更多信息,请参阅Providing Resources
【解决方案2】:

不同的设备可能有不同的大小限制。尝试使用:drawable-xxhdpi

有用的备忘单:http://i.stack.imgur.com/kV4Oh.png

【讨论】:

    【解决方案3】:

    为了管理Out Of Memory Error,您可能需要做的一件事是通过压缩来减小图像大小并将其保存在可绘制文件夹中。这对于减少应用程序大小和运行时的内存消耗非常有用。

    或者您可能需要使用以下类来减小图像大小纵横比。

    ImageResizer

      public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
                                                             int reqWidth, int reqHeight, boolean isLow) {
    
            // First decode with inJustDecodeBounds=true to check dimensions
            final BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            if (isLow) {
                options.inPreferredConfig = Bitmap.Config.RGB_565;
            }
            BitmapFactory.decodeResource(res, resId, options);
    
            // Calculate inSampleSize
            options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
    
            // Decode bitmap with inSampleSize set
            options.inJustDecodeBounds = false;
            return BitmapFactory.decodeResource(res, resId, options);
        }
    
    
        /**
         * Calculate an inSampleSize for use in a {@link BitmapFactory.Options} object when decoding
         * bitmaps using the decode* methods from {@link BitmapFactory}. This implementation calculates
         * the closest inSampleSize that is a power of 2 and will result in the final decoded bitmap
         * having a width and height equal to or larger than the requested width and height.
         *
         * @param options   An options object with out* params already populated (run through a decode*
         *                  method with inJustDecodeBounds==true
         * @param reqWidth  The requested width of the resulting bitmap
         * @param reqHeight The requested height of the resulting bitmap
         * @return The value to be used for inSampleSize
         */
        public static int calculateInSampleSize(BitmapFactory.Options options,
                                                int reqWidth, int reqHeight) {
            // BEGIN_INCLUDE (calculate_sample_size)
            // Raw height and width of image
            final int height = options.outHeight;
            final int width = options.outWidth;
            int inSampleSize = 1;
    
            if (height > reqHeight || width > reqWidth) {
    
                final int halfHeight = height / 2;
                final int halfWidth = width / 2;
    
                // Calculate the largest inSampleSize value that is a power of 2 and keeps both
                // height and width larger than the requested height and width.
                while ((halfHeight / inSampleSize) > reqHeight
                        && (halfWidth / inSampleSize) > reqWidth) {
                    inSampleSize *= 2;
                }
    
                // This offers some additional logic in case the image has a strange
                // aspect ratio. For example, a panorama may have a much larger
                // width than height. In these cases the total pixels might still
                // end up being too large to fit comfortably in memory, so we should
                // be more aggressive with sample down the image (=larger inSampleSize).
    
                long totalPixels = width * height / inSampleSize;
    
                // Anything more than 2x the requested pixels we'll sample down further
                final long totalReqPixelsCap = reqWidth * reqHeight * 2;
    
                while (totalPixels > totalReqPixelsCap) {
                    inSampleSize *= 2;
                    totalPixels /= 2;
                }
            }
            return inSampleSize;
            // END_INCLUDE (calculate_sample_size)
        }
    

    用法

       private Bitmap mBackground;
       private Drawable mBackgroundDrawable;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
                LinearLayout linearLayout = (LinearLayout) findViewById(R.id.parent);
                final Resources res = getResources();
                int[] dimensions = Util.getDisplayDimensions(this);
                mBackground = ImageResizer.decodeSampledBitmapFromResource(res, R.drawable.bg, 100, 100, false);
                mBackgroundDrawable = new BitmapDrawable(res, mBackground);
                linearLayout.setBackground(mBackgroundDrawable);
             }
    
        @Override
        protected void onDestroy() {
            recycle();
            super.onDestroy();
        }
    
        private void recycle() {
            if (mBackground != null) {
                mBackground.recycle();
                mBackground = null;
                if (mBackgroundDrawable != null)
                    mBackgroundDrawable = null;
    
            }
        }
    

    注意:如果您应用true 作为第三个参数,这有助于您使用Bitmap.Config.RGB_565 有效地减小图像大小。

             if (isLow) {
                 options.inPreferredConfig = Bitmap.Config.RGB_565;
               }
    

    最后,关于OOM的研究。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-08
      • 2013-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-17
      相关资源
      最近更新 更多