0

我尝试重新调整位图的大小,但有时我会遇到 OutOfMemoryError 崩溃。认为这条线是问题所在:

Bitmap.createBitmap(bm, 0, 0, width, height,
            matrix, false);

这是功能:

public static Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
    int width = bm.getWidth();
    int height = bm.getHeight();
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;
    // CREATE A MATRIX FOR THE MANIPULATION
    Matrix matrix = new Matrix();
    // RESIZE THE BIT MAP
    matrix.postScale(scaleWidth, scaleHeight);

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.JPEG, 100 , bos);
    byte[] bitmapdata = bos.toByteArray();
    ByteArrayInputStream bs = new ByteArrayInputStream(bitmapdata);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, width, height);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;

    bm = BitmapFactory.decodeStream(bs, null, options);


    // "RECREATE" THE NEW BITMAP
    return Bitmap.createBitmap(bm, 0, 0, width, height,
            matrix, false);
}

这是一个例外:

java.lang.OutOfMemoryError: Failed to allocate a 63489036 byte allocation with 16777120 free bytes and 54MB until OOM
   at dalvik.system.VMRuntime.newNonMovableArray(VMRuntime.java)
   at android.graphics.BitmapFactory.nativeDecodeStream(BitmapFactory.java)
   at android.graphics.BitmapFactory.decodeStreamInternal(BitmapFactory.java:863)
   at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:839)
   at utils.Utils.getResizedBitmap(Utils.java:573)
   at utils.Utils.getScaledBitmap(Utils.java:621)
   at com.meucci.IncomingCallActivitym.onActivityResult(IncomingCallActivitym.java:904)
   at android.app.Activity.dispatchActivityResult(Activity.java:6758)
   at android.app.ActivityThread.deliverResults(ActivityThread.java:4668)
   at android.app.ActivityThread.handleSendResult(ActivityThread.java:4715)
   at android.app.ActivityThread.access$1500(ActivityThread.java:198)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1725)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:145)
   at android.app.ActivityThread.main(ActivityThread.java:6837)
   at java.lang.reflect.Method.invoke(Method.java)
   at java.lang.reflect.Method.invoke(Method.java:372)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
4

1 回答 1

0

尝试这个,

private Bitmap resizeBitmap(Bitmap image) {
        int width = image.getWidth();
        int height = image.getHeight();
        float ratioBitmap = (float) width / (float) height;
        if (width > height) {
            ratioBitmap = (float) height / (float) width;
        }
        int targetHeight = MAX_IMAGE_SIZE;
        int targetWidth = (int) ((float) targetHeight * ratioBitmap);
//        Log.i(Constants.TAG, "old width = " + width + " new width = " + targetWidth);
//        Log.i(Constants.TAG, "old Height = " + height + " new Height = " + targetHeight);
        image = Bitmap.createScaledBitmap(image, targetWidth, targetHeight, true);
        return image;
    }
于 2016-04-12T07:24:36.567 回答