【问题标题】:OutOfMemory issue when using Bitmap in ImageView在 ImageView 中使用位图时出现 OutOfMemory 问题
【发布时间】:2014-01-10 05:56:00
【问题描述】:

我的 android 应用程序中有超过 8 个活动,所有屏幕都有很多 imageView,它们将用位图图像呈现

当我们打开所有屏幕(活动)时,它会正常工作几次,然后会出现内存不足错误,应用程序崩溃

这是我的代码如何显示图像,请建议如何避免这个/管理这个内存不足问题

XML:

<?xml version="1.0" encoding="utf-8"?>    
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
    android:layout_width="match_parent"    
    android:layout_height="match_parent"    
    android:orientation="vertical"    
    android:background="@drawable/img_background">    

   <ImageView    
        android:layout_width="match_parent"   
        android:layout_height="match_parent"    
        android:scaleType="fitCenter"    
        android:adjustViewBounds="true"       
        android:id="@+id/artifactImage"      
        android:onClick="displayFullScreenImage"/>      

</LinearLayout>    

活动:

ImageView artifact_Image = (ImageView) gridView.findViewById(R.id.artifactImage);     
artifact_Image.setImageBitmap(artifactImages[position]);       
artifact_Image.setDrawingCacheEnabled(false);

【问题讨论】:

    标签: android bitmap imageview out-of-memory


    【解决方案1】:

    您显示的图像可能占用了整个内存空间,这就是您收到此错误的原因。您可能想查看有关如何显示位图的文档:

    http://developer.android.com/training/displaying-bitmaps/index.html

    在您的情况下,您应该加载图像的缩小版本。查看此链接以获取示例代码:

    http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

    为了方便,我也将示例代码复制到这里。

    计算样本量:

    public static int calculateInSampleSize(
                BitmapFactory.Options options, int reqWidth, int reqHeight) {
        // 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;
            }
        }
    
        return inSampleSize;
    }
    

    解码位图:

    public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
            int reqWidth, int reqHeight) {
    
        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        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);
    }
    

    最后设置缩放位图

    mImageView.setImageBitmap(
        decodeSampledBitmapFromResource(getResources(), R.id.myimage, 100, 100));
    

    【讨论】:

    • 请注意,上述 Google 的 calculateInSampleSize 函数中存在一个错误,对于 inSampleSize 应该等于 2 的情况,它会错误地返回 1。
    【解决方案2】:

    以下是在您的 Android 应用程序中加载位图很棘手的一些原因:

    1. 移动设备通常具有受限的系统资源。 Android 设备可以为单个应用程序提供低至 16MB 的内存。 Android 兼容性定义文档 (CDD),第 3.7 节。虚拟机兼容性为各种屏幕尺寸和密度提供所需的最低应用程序内存。应优化应用程序以在此最小内存限制下执行。但是,请记住,许多设备都配置了更高的限制。

    2. 位图会占用大量内存,尤其是对于照片等丰富的图像。例如,Galaxy Nexus 上的相机可拍摄高达 2592x1936 像素(5 兆像素)的照片。如果使用的位图配置是 ARGB_8888(Android 2.3 以后的默认配置),则将此图像加载到内存中大约需要 19MB 内存(2592*1936*4 字节),会立即耗尽某些设备上的每个应用程序的限制。

    3. Android 应用程序 UI 经常需要同时加载多个位图。 ListView、GridView 和 ViewPager 等组件通常在屏幕上同时包含多个位图,而更多可能在屏幕外准备好在手指轻弹时显示。

    here 也是一个不错的博客,可以避免内存泄漏。

    如果你想加载多个位图而不出现内存错误,你应该使用image Loader library

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-19
      • 1970-01-01
      • 2019-08-27
      相关资源
      最近更新 更多