首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >BitmapFactory.decodeByteArray OutOfMemory

BitmapFactory.decodeByteArray OutOfMemory
EN

Stack Overflow用户
提问于 2015-07-13 15:34:04
回答 3查看 7.2K关注 0票数 3

我想从Camera.PictureCallback onPictureTaken得到一个Jpeg,但是当我这么做的时候

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

这是第一次出现在logcat上:

将堆增长到48.886MB,用于15728656字节的分配。

当我试图重拍一张图片时,这个应用程序会用这个堆栈跟踪崩溃:

代码语言:javascript
复制
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)

这是代码:

代码语言:javascript
复制
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后回收位图

这是可以的,但是当我试图显示路径上的图像时,我也有同样的错误。

EN

回答 3

Stack Overflow用户

发布于 2015-07-13 15:40:21

在Android中加载大位图是非常困难的--请看这个

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

票数 3
EN

Stack Overflow用户

发布于 2015-12-02 00:47:57

在这个http://developer.android.com/training/displaying-bitmaps/load-bitmap.html之后,我只是更改了decodeSampledBitmapFromResource静态方法,以适应decodeByteArray而不是decodeResource。我把这些方法放进了我的实用课。

代码语言:javascript
复制
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);
}

然后用您的代码进行以下操作:

代码语言:javascript
复制
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);


}

};

票数 3
EN

Stack Overflow用户

发布于 2015-07-13 16:15:33

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

代码语言:javascript
复制
bitmapImage.recycle();
bitmapImage = null;

如果您确实需要将其显示在屏幕上,那么尽可能地降低它的样本,而不会使它看起来很糟糕,并确保您没有保存对原始全尺寸图像的引用。还可以查看是否可以在从字节数组中获取图像时对图像进行压缩。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31387547

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档