2016-04-12 15 views
0

ビットマップのサイズを変更しようとしましたが、時々OutOfMemoryErrorクラッシュが発生しました。 は、この行が問題であることを考える:ビットマップのサイズをandroidで変更する - 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) 
+0

あなた、これを試してみてくださいビットマップサイズは大きく、クロップまたは圧縮のいずれか –

+0

アプリケーションタグの下のAndroidManifest.xmlファイルのlargeHeap = trueを追加 –

+0

品質を100から30に変更して圧縮サイズを縮小 –

答えて

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

しかし、私は特定の解像度にビットマップのサイズを変更したい(幅 - 高さ) – user3737110

+0

心配しないで、私はこれを試してみます。ありがとう! – user3737110

関連する問題