Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/208.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在android中调整位图大小-OutOfMemoryError异常_Android_Out Of Memory_Android Bitmap_Createbitmap - Fatal编程技术网

在android中调整位图大小-OutOfMemoryError异常

在android中调整位图大小-OutOfMemoryError异常,android,out-of-memory,android-bitmap,createbitmap,Android,Out Of Memory,Android Bitmap,Createbitmap,我试图重新调整位图的大小,但有时会出现OutOfMemoryError崩溃。 我认为这条线就是问题所在: Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false); 这就是功能: public static Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) { int width = bm.getWidth(); i

我试图重新调整位图的大小,但有时会出现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)
试试这个

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

您的位图大小很大。请在应用程序标记下的AndroidManifest.xml文件中通过剪切或压缩Add largeHeap=true来减小它。请将质量从100更改为30以减小压缩大小。可能存在重复,但我希望将位图大小调整为特定分辨率(宽度-高度)