BitmapFactory.decodeByteArray OutOfMemory

时间:2015-07-13 15:34:05

标签: java android bitmap

我试图从Camera.PictureCallback onPictureTaken获取Jpeg,但当我这样做时

bitmapImage = BitmapFactory.decodeByteArray(data, 0, data.length);

第一次出现在logcat上:

为15728656字节分配增加堆(frag case)到48.886MB

当我尝试重新拍摄照片时,应用程序会因此堆栈跟踪而崩溃:

java.lang.OutOfMemoryError
            at android.graphics.BitmapFactory.nativeDecodeByteArray(Native Method)
            at android.graphics.BitmapFactory.decodeByteArray(BitmapFactory.java:603)
            at android.graphics.BitmapFactory.decodeByteArray(BitmapFactory.java:626)
            at com.delsorboilario.brianzashop.Scatta$5.onPictureTaken(Scatta.java:216)
            at android.hardware.Camera$EventHandler.handleMessage(Camera.java:987)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5511)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
            at dalvik.system.NativeStart.main(Native Method)

这是代码:

android.hardware.Camera.PictureCallback jpegCallback = new android.hardware.Camera.PictureCallback() {
    @Override
    public void onPictureTaken(byte[] data, android.hardware.Camera camera) {

         bitmapImage = BitmapFactory.decodeByteArray(data, 0, data.length);


        Uri tempUri = getImageUri(getApplicationContext(), bitmapImage);

        File finalFile = new File(getRealPathFromURI(tempUri));

        System.out.println("aaaaaa "+finalFile);


    }
};

修改

根据MicheleLacorte的建议,我在尝试File finalFile

之后尝试回收Bitmap

并且没问题,但是当我尝试从该路径显示图像时,我也犯了同样的错误。

3 个答案:

答案 0 :(得分:3)

在Android中加载大型位图非常困难 看看这个

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

答案 1 :(得分:2)

http://developer.android.com/training/displaying-bitmaps/load-bitmap.html之后,我刚刚更改了decodeSampledBitmapFromResource静态方法以适应decodeByteArray而不是decodeResource。我把方法放在我的util类中。

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(byte[] data, int   reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    //options.inPurgeable = true; 
    BitmapFactory.decodeByteArray(data, 0, data.length, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeByteArray(data, 0, data.length, options);
}

然后使用您的代码尝试:

android.hardware.Camera.PictureCallback jpegCallback = new  android.hardware.Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, android.hardware.Camera camera) {

     //bitmapImage = BitmapFactory.decodeByteArray(data, 0, data.length);
     // Utils is my utility class
     bitmapImage = Utils.decodeSampledBitmapFromResource(data, 50,50);


    Uri tempUri = getImageUri(getApplicationContext(), bitmapImage);

    File finalFile = new File(getRealPathFromURI(tempUri));

    System.out.println("aaaaaa "+finalFile);


}

};

答案 2 :(得分:1)

你说这是你拍第二张照片的时候有问题吗?在允许用户重新拍摄照片之前,您需要对第一张照片执行某些操作以清除某些内存。如果您不打算继续显示它,请将其保存到磁盘上的缓存文件中,然后使用

释放内存资源。
bitmapImage.recycle();
bitmapImage = null;

如果您确实需要将其显示在屏幕上,请尽可能地对其进行下采样,而不要让它看起来很糟糕,并确保您没有保留对原始全尺寸图像的引用。另外,看看你是否可以在从字节数组中获取图像时对图像进行一些压缩。