Android创建位图+裁剪导致OutOfMemory错误(最终)



在上传到远程服务器之前,我在我的应用程序中拍摄了3张照片。输出是一个byteArray。我目前正在将这个byteArray转换为位图,对其进行裁剪(裁剪中心正方形)。我最终用完内存(即退出应用程序回来后,执行相同的步骤)。我试图使用BitmapFactory重新使用位图对象。android开发指南

中提到的选项

https://www.youtube.com/watch?v=_ioFW3cyRV0&列表= LLntRvRsglL14LdaudoRQMHg&指数= 2

https://www.youtube.com/watch?v=rsQet4nBVi8&列表= LLntRvRsglL14LdaudoRQMHg&指数= 3

这是我保存相机拍摄的图像时调用的函数。

public void saveImageToDisk(Context context, byte[] imageByteArray, String photoPath, BitmapFactory.Options options) {
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeByteArray(imageByteArray, 0, imageByteArray.length, options);
    int imageHeight = options.outHeight;
    int imageWidth = options.outWidth;
    int dimension = getSquareCropDimensionForBitmap(imageWidth, imageHeight);
    Log.d(TAG, "Width : " + dimension);
    Log.d(TAG, "Height : " + dimension);
    //bitmap = cropBitmapToSquare(bitmap);
    options.inJustDecodeBounds = false;
    Bitmap bitmap = BitmapFactory.decodeByteArray(imageByteArray, 0,
            imageByteArray.length, options);
    options.inBitmap = bitmap;
    bitmap = ThumbnailUtils.extractThumbnail(bitmap, dimension, dimension,
            ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
    options.inSampleSize = 1;
    Log.d(TAG, "After square crop Width : " + options.inBitmap.getWidth());
    Log.d(TAG, "After square crop Height : " + options.inBitmap.getHeight());
    byte[] croppedImageByteArray = convertBitmapToByteArray(bitmap);
    options = null;
    File photo = new File(photoPath);
    if (photo.exists()) {
        photo.delete();
    }

    try {
        FileOutputStream e = new FileOutputStream(photo.getPath());
        BufferedOutputStream bos = new BufferedOutputStream(e);
        bos.write(croppedImageByteArray);
        bos.flush();
        e.getFD().sync();
        bos.close();
    } catch (IOException e) {
    }
}

public int getSquareCropDimensionForBitmap(int width, int height) {
    //If the bitmap is wider than it is tall
    //use the height as the square crop dimension
    int dimension;
    if (width >= height) {
        dimension = height;
    }
    //If the bitmap is taller than it is wide
    //use the width as the square crop dimension
    else {
        dimension = width;
    }
    return dimension;
}

 public Bitmap cropBitmapToSquare(Bitmap source) {
    int h = source.getHeight();
    int w = source.getWidth();
    if (w >= h) {
        source = Bitmap.createBitmap(source, w / 2 - h / 2, 0, h, h);
    } else {
        source = Bitmap.createBitmap(source, 0, h / 2 - w / 2, w, w);
    }
    Log.d(TAG, "After crop Width : " + source.getWidth());
    Log.d(TAG, "After crop Height : " + source.getHeight());
    return source;
}

我如何正确地回收或重用位图,因为现在我得到了OutOfMemory错误?

UPDATE:

在执行Colin的解决方案之后。我遇到了ArrayIndexOutOfBoundsException.

我的日志在

下面
08-26 01:45:01.895    3600-3648/com.test.test E/AndroidRuntime﹕ FATAL EXCEPTION: pool-3-thread-1
Process: com.test.test, PID: 3600
java.lang.ArrayIndexOutOfBoundsException: length=556337; index=556337
        at com.test.test.helpers.Utils.test(Utils.java:197)
        at com.test.test.fragments.DemoCameraFragment.saveImageToDisk(DemoCameraFragment.java:297)
        at com.test.test.fragments.DemoCameraFragment_.access$101(DemoCameraFragment_.java:30)
        at com.test.test.fragments.DemoCameraFragment_$5.execute(DemoCameraFragment_.java:159)
        at org.androidannotations.api.BackgroundExecutor$Task.run(BackgroundExecutor.java:401)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
        at java.util.concurrent.FutureTask.run(FutureTask.java:237)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:152)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:265)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
        at java.lang.Thread.run(Thread.java:818)

p。S:我以前想过裁剪byteArrays,但是我不知道怎么实现。

实际上你不需要做任何位图转换。记住,您的位图图像数据是RGBA_8888格式的。这意味着每4个连续字节代表一个像素。比如:

// helpers to make sanity
int halfWidth = imgWidth >> 1;
int halfHeight = imgHeight >> 1;
int halfDim = dimension >> 1;
// get our min and max crop locations
int minX = halfWidth - halfDim;
int minY = halfHeight - halfDim;
int maxX = halfWidth + halfDim;
int maxY = halfHeight + halfDim;
// allocate our thumbnail; It's WxH*(4 bits per pixel)
byte[] outArray = new byte[dimension * dimension * 4]
int outPtr = 0;
for(int y = minY; y< maxY; y++)
{
    for(int x = minX; x < maxX; x++)
    {
        int srcLocation = (y * imgWidth) + (x * 4);
        outArray[outPtr + 0] = imageByteArray[srcLocation +0]; // read R
        outArray[outPtr + 1] = imageByteArray[srcLocation +1]; // read G
        outArray[outPtr + 2] = imageByteArray[srcLocation +2]; // read B
        outArray[outPtr + 3] = imageByteArray[srcLocation +3]; // read A
        outPtr+=4;
    }   
}
//outArray now contains the cropped pixels.

最终的结果是,你可以通过复制你正在寻找的像素手工裁剪,而不是分配一个新的位图对象,然后将其转换回字节数组。

= =编辑:

实际;上述算法假设您的输入数据是原始的RGBA_8888像素数据。但听起来,您的输入字节数组是编码的JPG数据。因此,第二个decodeByteArray实际上是将JPG文件解码为RGBA_8888格式。如果是这种情况,重新调整大小的正确方法是使用"在android上最有效地调整位图大小的方法"中描述的技术,因为您正在处理编码数据。

尝试为null设置越来越多的变量-这有助于回收内存;

 byte[] croppedImageByteArray = convertBitmapToByteArray(bitmap);

:

bitmap= null;

 FileOutputStream e = new FileOutputStream(photo.getPath());

photo = null;

之后
 try {
        FileOutputStream e = new FileOutputStream(photo.getPath());
        BufferedOutputStream bos = new BufferedOutputStream(e);
        bos.write(croppedImageByteArray);
        bos.flush();
        e.getFD().sync();
        bos.close();
    } catch (IOException e) {
    }

:

e = null;
bos = null;

编辑# 1

如果这没有帮助,你唯一真正的解决方案实际上是使用内存监视器。要了解更多信息,请访问这里和这里

p。还有一个非常黑暗的解决方案,非常黑暗的解决方案。只适合那些知道如何穿越记忆黑暗角落的人。但你必须自己走这条路。

相关内容

最新更新