Android位图OutOfMemoryError

Android位图OutOfMemoryError,android,bitmap,imageview,out-of-memory,Android,Bitmap,Imageview,Out Of Memory,我的VSD220中出现OutOfMemoryError(这是一款基于22英寸Android的多功能一体机) 我真的不知道该怎么办,因为这张图像的分辨率低于最大分辨率。图像大小大约是(1000x1000),显示器是1920x1080 有什么帮助吗? (foreach循环用于大约20个元素,在6个或7个循环后会中断。) 非常感谢 Ezequiel.是否确实要加载同一位图20次?是否要加载一次并将其设置在循环中 尽管如此,无论屏幕分辨率如何,加载1000x1000像素图像都不能保证正常工作。请记住,1

我的VSD220中出现OutOfMemoryError(这是一款基于22英寸Android的多功能一体机)

我真的不知道该怎么办,因为这张图像的分辨率低于最大分辨率。图像大小大约是(1000x1000),显示器是1920x1080

有什么帮助吗? (foreach循环用于大约20个元素,在6个或7个循环后会中断。)

非常感谢


Ezequiel.

是否确实要加载同一位图20次?是否要加载一次并将其设置在循环中

尽管如此,无论屏幕分辨率如何,加载1000x1000像素图像都不能保证正常工作。请记住,1000x1000像素图像占用1000x1000x4字节=~4MB(如果加载为ARGB_8888)。如果堆内存不完整/太小,则可能没有足够的空间加载位图。您可能需要查看类并尝试使用和

我建议您使用DigCamara的建议,确定大小并加载接近该大小的降采样图像(我说几乎是因为使用该技术无法获得确切的大小),或者尝试加载全尺寸图像,然后递归地增加样本大小(以两倍的倍数获得最佳结果)在达到最大样本大小或加载图像之前:

/**
 * Load a bitmap from a stream using a specific pixel configuration. If the image is too
 * large (ie causes an OutOfMemoryError situation) the method will iteratively try to
 * increase sample size up to a defined maximum sample size. The sample size will be doubled
 * each try since this it is recommended that the sample size should be a factor of two
 */
public Bitmap getAsBitmap(InputStream in, BitmapFactory.Config config, int maxDownsampling) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 1;   
    options.inPreferredConfig = config;
    Bitmap bitmap = null;
    // repeatedly try to the load the bitmap until successful or until max downsampling has been reached
    while(bitmap == null && options.inSampleSize <= maxDownsampling) {
        try {
            bitmap = BitmapFactory.decodeStream(in, null, options);
            if(bitmap == null) {
                // not sure if there's a point in continuing, might be better to exit early
                options.inSampleSize *= 2;
            }
        }
        catch(Exception e) {
            // exit early if we catch an exception, for instance an IOException
            break;
        }
        catch(OutOfMemoryError error) {
            // double the sample size, thus reducing the memory needed by 50%
            options.inSampleSize *= 2;
        }
    }
    return bitmap;
}
/**
*使用特定的像素配置从流中加载位图。如果图像太
*大型(即导致OutOfMemoryError情况)方法将迭代尝试
*将样本大小增加到定义的最大样本大小。样本大小将加倍
*每次尝试后,建议样本量应为两倍
*/
公共位图getAsBitmap(InputStream in,BitmapFactory.Config配置,int maxDownsampling){
BitmapFactory.Options=new-BitmapFactory.Options();
options.inSampleSize=1;
options.inPreferredConfig=config;
位图=空;
//反复尝试加载位图,直到成功或达到最大下采样

while(bitmap==null&&options.inSampleSize是否确实要加载同一位图20次?是否要加载一次并将其设置在循环内

尽管如此,无论屏幕分辨率如何,加载1000x1000像素图像都不能保证正常工作。请记住,1000x1000像素图像占用1000x1000x4字节=~4MB(如果加载为ARGB_8888)。如果堆内存不完整/太小,则可能没有足够的空间加载位图。您可能需要查看类并尝试使用和

我建议您使用DigCamara的建议,确定大小并加载接近该大小的降采样图像(我说几乎是因为使用该技术无法获得确切的大小),或者尝试加载全尺寸图像,然后递归地增加样本大小(以两倍的倍数获得最佳结果)在达到最大样本大小或加载图像之前:

/**
 * Load a bitmap from a stream using a specific pixel configuration. If the image is too
 * large (ie causes an OutOfMemoryError situation) the method will iteratively try to
 * increase sample size up to a defined maximum sample size. The sample size will be doubled
 * each try since this it is recommended that the sample size should be a factor of two
 */
public Bitmap getAsBitmap(InputStream in, BitmapFactory.Config config, int maxDownsampling) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 1;   
    options.inPreferredConfig = config;
    Bitmap bitmap = null;
    // repeatedly try to the load the bitmap until successful or until max downsampling has been reached
    while(bitmap == null && options.inSampleSize <= maxDownsampling) {
        try {
            bitmap = BitmapFactory.decodeStream(in, null, options);
            if(bitmap == null) {
                // not sure if there's a point in continuing, might be better to exit early
                options.inSampleSize *= 2;
            }
        }
        catch(Exception e) {
            // exit early if we catch an exception, for instance an IOException
            break;
        }
        catch(OutOfMemoryError error) {
            // double the sample size, thus reducing the memory needed by 50%
            options.inSampleSize *= 2;
        }
    }
    return bitmap;
}
/**
*使用特定的像素配置从流中加载位图。如果图像太
*大型(即导致OutOfMemoryError情况)方法将迭代尝试
*将样本大小增加到定义的最大样本大小。样本大小将加倍
*每次尝试后,建议样本量应为两倍
*/
公共位图getAsBitmap(InputStream in,BitmapFactory.Config配置,int maxDownsampling){
BitmapFactory.Options=new-BitmapFactory.Options();
options.inSampleSize=1;
options.inPreferredConfig=config;
位图=空;
//反复尝试加载位图,直到成功或达到最大下采样

虽然(bitmap==null&&options.inSampleSize您应该查看的培训文档。根据您的操作系统版本,您可以使用不同的技术来管理更多位图,但您可能仍需要更改代码

特别是,您可能需要在“”中使用代码的修订版本,但我至少发现这一部分特别有用:

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) {

        // Calculate ratios of height and width to requested height and width
        final int heightRatio = Math.round((float) height / (float) reqHeight);
        final int widthRatio = Math.round((float) width / (float) reqWidth);

        // Choose the smallest ratio as inSampleSize value, this will guarantee
        // a final image with both dimensions larger than or equal to the
        // requested height and width.
        inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
    }

    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);
}

您应该查看的培训文档。根据您的操作系统版本,您可以使用不同的技术来管理更多位图,但您可能仍需要更改代码

特别是,您可能需要在“”中使用代码的修订版本,但我至少发现这一部分特别有用:

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) {

        // Calculate ratios of height and width to requested height and width
        final int heightRatio = Math.round((float) height / (float) reqHeight);
        final int widthRatio = Math.round((float) width / (float) reqWidth);

        // Choose the smallest ratio as inSampleSize value, this will guarantee
        // a final image with both dimensions larger than or equal to the
        // requested height and width.
        inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
    }

    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);
}

我不同意将此标记为副本。在这个问题中,相同的图像重复加载到循环中。即使图像非常小,如果迭代次数足够多,也会出现内存不足错误。我不同意将此标记为副本。在这个问题中,相同的图像重复加载到循环中。即使如果迭代次数足够多,你会得到内存不足的错误。对不起,我不想加载同一个图像20次,我想简化这个例子,我的真实代码由一个路径数组组成。所以图像不一样,但大小不同。我现在正在尝试你的答案,伙计们。我会在之后再次评论r这个。非常感谢!!非常感谢你们两位。这个缩略图很有用,现在我必须显示一个全屏图像(本例中为1920x1024),并制作它们的图库。我将尝试阅读android位图部分!抱歉,我不想加载相同的图像20次,我想简化示例,我的真实代码由一个路径数组组成。因此图像不一样,但大小不同。我正在尝试你们的答案,伙计们。之后我将再次发表评论。谢谢非常感谢!!非常感谢你们两位。这个缩略图很有用,现在我必须显示一个全屏图像(本例中为1920x1024),并制作一个它们的图库。我将在阅读android位图部分时尝试这样做!在这里尝试{//代码,这会导致