OutOfMemory同时位图decodeByteArray

时间:2015-09-15 09:13:51

标签: android out-of-memory android-bitmap

我正在尝试将字节数组加载到位图中。我已经尝试缩小图像以使其更小但我仍然得到OutOfMemory错误。如何确保人们不会在此处收到OutOfMemory错误?

我用来创建位图的代码:

private void rotatePicture(int rotation, byte[] data, ImageView photoImageView) {
    BitmapFactory.Options opts = new BitmapFactory.Options();
    opts.inSampleSize = 2; // Power of 2
    Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, opts);
...

stacktrace:

09-15 11:09:28.182  11831-11831/com.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.OutOfMemoryError
            at android.graphics.BitmapFactory.nativeDecodeByteArray(Native Method)
            at android.graphics.BitmapFactory.decodeByteArray(BitmapFactory.java:522)
            at com.app.SquareCamera.EditSavePhotoFragment.rotatePicture(EditSavePhotoFragment.java:85)
            at com.app.SquareCamera.EditSavePhotoFragment.onViewCreated(EditSavePhotoFragment.java:72)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:973)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
            at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:458)
            at android.os.Handler.handleCallback(Handler.java:730)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:176)
            at android.app.ActivityThread.main(ActivityThread.java:5419)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
            at dalvik.system.NativeStart.main(Native Method)

修改

我现在尝试使用以下代码来使用http://developer.android.com/training/displaying-bitmaps/load-bitmap.html中的示例。但我仍然在 return BitmapFactory.decodeByteArray(data, 0, data.length, options);方法的decodeSampledBitmapFromByteArray上收到OutOfMemory错误。

private void rotatePicture(int rotation, byte[] data, ImageView photoImageView) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeByteArray(data, 0, data.length, options);
    int imageHeight = options.outHeight;
    int imageWidth = options.outWidth;

    Bitmap bitmap = decodeSampledBitmapFromByteArray(data, imageWidth, imageHeight);
    ...
}


public static Bitmap decodeSampledBitmapFromByteArray(byte[] data,
                                                     int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = 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);
}

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

2 个答案:

答案 0 :(得分:1)

您尝试将图像缩减为当前尺寸而不是缩小尺寸

int imageHeight = options.outHeight;
int imageWidth = options.outWidth;

Bitmap bitmap = decodeSampledBitmapFromByteArray(data, imageWidth, imageHeight);
...

如果你改成这样的东西

int imageHeight = options.outHeight;
int imageWidth = options.outWidth;

// calculate maxHeight and maxWidth from screen resolution

while ((imageHeight > maxHeight) || (imageWidth > maxWidth)) {
   imageHeight /= 2; imageWidth /= 2;
}

Bitmap bitmap = decodeSampledBitmapFromByteArray(data, imageWidth, imageHeight);

答案 1 :(得分:0)

您好请根据您的需要使用以下功能

public static byte[] getByteOfImage(String p_fileName) throws Exception
    {
        Bitmap m_bitmap = null;
        byte[] m_bitmapdata1 = null;
        try
        {
            m_bitmap = decodeFile(new File(p_fileName), 100, 100);
        }
        catch (Throwable m_throwable)
        {

        }

        ByteArrayOutputStream m_bos = new ByteArrayOutputStream();
        m_bitmap.compress(Bitmap.CompressFormat.JPEG, 90, m_bos);
        m_bitmapdata1 = m_bos.toByteArray();
        m_bitmap.recycle();


        return m_bitmapdata1;
    }




 public static Bitmap decodeFile(File file, int width, int height)
    {
        try
        {
            //Decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(new FileInputStream(file), null, o);
            int scale = 1;

            //The new size we want to scale to
            final int REQUIRED_WIDTH = width;
            final int REQUIRED_HIGHT = height;



            //Find the correct scale value. It should be the power of 2.
            while (o.outWidth / scale / 2 >= REQUIRED_WIDTH && o.outHeight / scale / 2 >= REQUIRED_HIGHT)
            {
                scale *= 2;
            }

            //Decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            return BitmapFactory.decodeStream(new FileInputStream(file), null, o2);
        }
        catch (FileNotFoundException e)
        {

        }
        return null;
    }
相关问题