Example usage for java.lang OutOfMemoryError printStackTrace

List of usage examples for java.lang OutOfMemoryError printStackTrace

Introduction

In this page you can find the example usage for java.lang OutOfMemoryError printStackTrace.

Prototype

public void printStackTrace() 

Source Link

Document

Prints this throwable and its backtrace to the standard error stream.

Usage

From source file:Main.java

public static Bitmap createBitmapSafely(int width, int height, Bitmap.Config config, int retryCount) {
    try {//from  w  ww .j a v a  2  s .c o  m
        return Bitmap.createBitmap(width, height, config);
    } catch (OutOfMemoryError e) {
        e.printStackTrace();
        if (retryCount > 0) {
            System.gc();
            return createBitmapSafely(width, height, config, retryCount - 1);
        }
        return null;
    }
}

From source file:Main.java

public static Bitmap getRoundedCornerBitmap(final Bitmap bitmap, final float roundPx) {

    if (bitmap != null) {
        try {/*from w ww  .j  av  a 2s .co  m*/
            final Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
            Canvas canvas = new Canvas(output);

            final Paint paint = new Paint();
            final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
            final RectF rectF = new RectF(rect);

            paint.setAntiAlias(true);
            canvas.drawARGB(0, 0, 0, 0);
            canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

            paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
            canvas.drawBitmap(bitmap, rect, rect, paint);

            return output;

        } catch (OutOfMemoryError e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return bitmap;
}

From source file:Main.java

public static BitmapDrawable loadAppIcon(String iconPath, Context context) {
    try {/*ww w  . ja  v a2 s  .co m*/
        Bitmap bitmap = null;
        Bitmap newBitmap = null;
        if (iconPath != null && !"".equals(iconPath)) {
            bitmap = BitmapFactory.decodeFile(iconPath);
        }

        if (bitmap == null || bitmap.getWidth() <= 0 || bitmap.getHeight() <= 0) {
            Log.e("RecommAppsUtils", iconPath + " is not exist");
            return null;
        }
        int densityDpi = context.getResources().getDisplayMetrics().densityDpi;
        float scale = densityDpi / STANDARD_DENSITYDPI;
        newBitmap = zoomBitmap(bitmap, (int) (APP_ICON_WIDTH * scale), (int) (APP_ICON_HEIGHT * scale));
        // Log.d("RecommAppsUtils", "densityDpi value : " + densityDpi);
        return new BitmapDrawable(context.getResources(), newBitmap);
    } catch (OutOfMemoryError error) {
        error.printStackTrace();
    }
    return null;
}

From source file:Main.java

/**
 * Rotates a bitmap by a certain angle//  w ww  .j a va 2  s  . c  o m
 *
 * @param pSourceBitmap
 *        The bitmap that needs to be rotated
 * @param pAngle
 *        The angle the bitmap should be rotated to
 * @return A rotated bitmap
 */
private static Bitmap rotateImage(Bitmap pSourceBitmap, float pAngle) {
    Bitmap bitmap = null;
    Matrix matrix = new Matrix();
    matrix.postRotate(pAngle);
    try {
        bitmap = Bitmap.createBitmap(pSourceBitmap, 0, 0, pSourceBitmap.getWidth(), pSourceBitmap.getHeight(),
                matrix, true);
    } catch (OutOfMemoryError err) {
        err.printStackTrace();
    }
    return bitmap;
}

From source file:Main.java

public static boolean saveBitmap(Bitmap aBmp, String aPath) {
    if (aBmp == null || aPath == null) {
        return false;
    }/*from w ww.j  a  v a  2 s. com*/
    FileOutputStream fos = null;
    ByteArrayOutputStream baos = null;
    boolean result;
    try {
        File file = new File(aPath);
        if (!file.exists()) {
            file.createNewFile();
        }
        fos = new FileOutputStream(file);
        baos = new ByteArrayOutputStream();
        aBmp.compress(Bitmap.CompressFormat.PNG, 100, baos); //SUPPRESS CHECKSTYLE
        fos.write(baos.toByteArray());
        baos.flush();
        fos.flush();

        result = true;
    } catch (OutOfMemoryError e) {
        e.printStackTrace();
        result = false;
    } catch (Exception e) {
        e.printStackTrace();
        result = false;
    } finally {
        try {
            if (baos != null) {
                baos.close();
            }
            if (fos != null) {
                fos.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return result;
}

From source file:Main.java

public static Bitmap normalizeExifRotateBitmap(Bitmap bitmap, int orientation) {

    Matrix matrix = new Matrix();
    switch (orientation) {
    case ExifInterface.ORIENTATION_NORMAL:
        return bitmap;
    case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
        matrix.setScale(-1, 1);//from  w  ww .  j  av a 2s  .co m
        break;
    case ExifInterface.ORIENTATION_ROTATE_180:
        matrix.setRotate(180);
        break;
    case ExifInterface.ORIENTATION_FLIP_VERTICAL:
        matrix.setRotate(180);
        matrix.postScale(-1, 1);
        break;
    case ExifInterface.ORIENTATION_TRANSPOSE:
        matrix.setRotate(90);
        matrix.postScale(-1, 1);
        break;
    case ExifInterface.ORIENTATION_ROTATE_90:
        matrix.setRotate(90);
        break;
    case ExifInterface.ORIENTATION_TRANSVERSE:
        matrix.setRotate(-90);
        matrix.postScale(-1, 1);
        break;
    case ExifInterface.ORIENTATION_ROTATE_270:
        matrix.setRotate(-90);
        break;
    default:
        return bitmap;
    }
    try {
        Bitmap bmRotated = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix,
                true);
        bitmap.recycle();
        return bmRotated;
    } catch (OutOfMemoryError e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static Bitmap safeDecodeBitmap(String bmpFile, BitmapFactory.Options options) {
    BitmapFactory.Options optsTmp = options;
    if (optsTmp == null) {
        optsTmp = new BitmapFactory.Options();
        optsTmp.inSampleSize = 1;/*from   ww w  .  jav  a2 s.  c o  m*/
    }

    Bitmap bmp = null;
    FileInputStream fis = null;

    int i = 1;
    while (i < 5) {
        try {
            fis = new FileInputStream(bmpFile);
            bmp = BitmapFactory.decodeStream(fis, null, optsTmp);
            break;
        } catch (OutOfMemoryError error) {
            error.printStackTrace();

            optsTmp.inSampleSize *= 2;

            try {
                if (fis != null) {
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

            ++i;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            break;
        }
    }

    return bmp;
}

From source file:Main.java

public static Bitmap resIdToBitmap(Resources res, int resId) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inScaled = false;//  w ww .  j a v  a  2  s  . c  om
    options.inDither = false;
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    options.inPreferQualityOverSpeed = true;
    try {
        return BitmapFactory.decodeResource(res, resId, options);
    } catch (OutOfMemoryError e1) {
        e1.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static Bitmap rotateBitmap(Bitmap bitmap, int exifOrientation) {
    Matrix matrix = new Matrix();
    switch (exifOrientation) {
    case ExifInterface.ORIENTATION_NORMAL:
    default://from   w  w w  .  j ava2 s  .  c o  m
        return bitmap;
    case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
        matrix.setScale(-1, 1);
        break;
    case ExifInterface.ORIENTATION_ROTATE_180:
        matrix.setRotate(180);
        break;
    case ExifInterface.ORIENTATION_FLIP_VERTICAL:
        matrix.setRotate(180);
        matrix.postScale(-1, 1);
        break;
    case ExifInterface.ORIENTATION_TRANSPOSE:
        matrix.setRotate(90);
        matrix.postScale(-1, 1);
        break;
    case ExifInterface.ORIENTATION_ROTATE_90:
        matrix.setRotate(90);
        break;
    case ExifInterface.ORIENTATION_TRANSVERSE:
        matrix.setRotate(-90);
        matrix.postScale(-1, 1);
        break;
    case ExifInterface.ORIENTATION_ROTATE_270:
        matrix.setRotate(-90);
        break;
    }

    try {
        return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);

    } catch (OutOfMemoryError e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static Bitmap getBitmap(String path, int requiredSize) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;//from w  ww. ja  va  2s . c o  m
    BitmapFactory.decodeFile(path, options);
    int scale = 1;

    while (options.outWidth / scale > requiredSize && options.outHeight / scale > requiredSize)
        scale *= 2;

    options.inSampleSize = scale;
    options.inJustDecodeBounds = false;
    options.inDither = false;
    options.inPurgeable = true;
    options.inInputShareable = true;
    options.inTempStorage = new byte[32 * 1024];

    Bitmap result = null;
    File file = new File(path);
    FileInputStream fs = null;
    try {
        fs = new FileInputStream(file);

        try {
            result = BitmapFactory.decodeFileDescriptor(fs.getFD(), null, options);
        } catch (OutOfMemoryError oom) {
            oom.printStackTrace();

            try {
                options.inSampleSize *= 4;
                result = BitmapFactory.decodeFileDescriptor(fs.getFD(), null, options);
            } catch (OutOfMemoryError oom1) {
                oom.printStackTrace();
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (fs != null)
            try {
                fs.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
    }

    return result;
}