Example usage for android.graphics Bitmap recycle

List of usage examples for android.graphics Bitmap recycle

Introduction

In this page you can find the example usage for android.graphics Bitmap recycle.

Prototype

public void recycle() 

Source Link

Document

Free the native object associated with this bitmap, and clear the reference to the pixel data.

Usage

From source file:Main.java

public final static Bitmap rotate(Bitmap b, float degrees) {
    if (degrees != 0 && b != null) {
        Matrix m = new Matrix();
        m.setRotate(degrees, (float) b.getWidth() / 2, (float) b.getHeight() / 2);

        Bitmap b2 = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), m, true);
        if (b != b2) {
            b.recycle();
            b = b2;/*from w  w  w  . j  ava2s . co m*/
        }

    }
    return b;
}

From source file:Main.java

public static Bitmap resizeBitmapToFitWidth(Bitmap bm, int width) {
    int oriWidth = bm.getWidth();
    int oriHeight = bm.getHeight();
    if (oriWidth < width) {
        return bm;
    } else {// ww  w  .  ja  v  a  2 s.  co m
        int height = (int) ((float) width / oriWidth * oriHeight);
        Bitmap tmp = Bitmap.createScaledBitmap(bm, width, height, false);
        bm.recycle();
        return tmp;
    }
}

From source file:Main.java

public static Bitmap rotation90(Bitmap bitmap) {
    if (bitmap == null)
        return null;
    if (bitmap.isRecycled())
        return null;
    Bitmap bmp = null;/*from  ww  w.j a va  2s. c o  m*/
    Matrix matrix = new Matrix();
    matrix.postRotate(90);
    bmp = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false);
    bitmap.recycle();
    return bmp;
}

From source file:Main.java

public static Bitmap big(Bitmap bitmap, float scale) {
    if (bitmap == null) {
        return null;
    }/*from   www . ja v a2 s  . c  o m*/
    Matrix matrix = new Matrix();
    matrix.postScale(scale, scale);
    Bitmap resizeBmp = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    if (!bitmap.isRecycled()) {
        bitmap.recycle();
    }
    return resizeBmp;
}

From source file:Main.java

public static Bitmap scaleBitmap(int height, int width, String pathImage) {
    Bitmap desBitmap = null;//from  w  ww .j a  va  2 s  . co m
    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(pathImage, bmOptions);
    int photoW = bmOptions.outWidth;
    int photoH = bmOptions.outHeight;

    // Determine how much to scale down the image
    int scaleFactor = Math.min(photoW / width, photoH / height);

    // Decode the image file into a Bitmap sized to fill the View
    bmOptions.inJustDecodeBounds = false;
    bmOptions.inSampleSize = scaleFactor << 1;
    bmOptions.inPurgeable = true;

    Bitmap bitmap = BitmapFactory.decodeFile(pathImage, bmOptions);

    Matrix mtx = new Matrix();
    mtx.postRotate(90);
    // Rotating Bitmap
    Bitmap rotatedBMP = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), mtx, true);

    if (rotatedBMP != bitmap) {
        bitmap.recycle();
    }

    return rotatedBMP;
}

From source file:Main.java

public static byte[] toByteArray(final Bitmap bmp, boolean recycle) {
    byte[] result = null;
    if (bmp != null) {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.JPEG, 85, output);
        result = output.toByteArray();/*from   ww w.java  2  s .  co m*/
        if (recycle) {
            bmp.recycle();
        }
    }
    return result;
}

From source file:Main.java

public static Bitmap rotateBitmap(Bitmap source, int angle) {
    Matrix matrix = new Matrix();
    if (angle > 0)
        matrix.setRotate(angle);/*from   ww  w  . j  a v  a  2s.co  m*/
    Bitmap bitmap = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
    if (bitmap != source && !source.isRecycled())
        source.recycle();
    return bitmap;
}

From source file:Main.java

public static Bitmap decodeScaleImage(String imagePath, int outWidth, int outHeight) {
    BitmapFactory.Options localOptions = new BitmapFactory.Options();
    localOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(imagePath, localOptions);
    int i = calculateInSampleSize(localOptions, outWidth, outHeight);
    localOptions.inSampleSize = i;//from   w w  w .j a v  a2 s  . co  m
    localOptions.inJustDecodeBounds = false;
    Bitmap localBitmap1 = BitmapFactory.decodeFile(imagePath, localOptions);
    int j = getPictureDegree(imagePath);
    Bitmap localBitmap2 = null;
    if ((localBitmap1 != null) && (j != 0)) {
        localBitmap2 = rotaingImageView(j, localBitmap1);
        localBitmap1.recycle();
        localBitmap1 = null;
        return localBitmap2;
    }
    return localBitmap1;
}

From source file:com.wrapp.android.webimage.ImageDownloader.java

public static boolean loadImage(final String imageKey, final URL imageUrl) {
    HttpEntity responseEntity = null;/*from ww  w.j  a  va 2s  .co m*/
    InputStream contentInputStream = null;

    try {
        final String imageUrlString = imageUrl.toString();
        if (imageUrlString == null || imageUrlString.length() == 0) {
            throw new Exception("Passed empty URL");
        }
        LogWrapper.logMessage("Requesting image '" + imageUrlString + "'");
        final HttpClient httpClient = new DefaultHttpClient();
        final HttpParams httpParams = httpClient.getParams();
        httpParams.setParameter(CoreConnectionPNames.SO_TIMEOUT, CONNECTION_TIMEOUT_IN_MS);
        httpParams.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, CONNECTION_TIMEOUT_IN_MS);
        httpParams.setParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, DEFAULT_BUFFER_SIZE);
        final HttpGet httpGet = new HttpGet(imageUrlString);
        final HttpResponse response = httpClient.execute(httpGet);

        responseEntity = response.getEntity();
        if (responseEntity == null) {
            throw new Exception("No response entity for image: " + imageUrl.toString());
        }
        contentInputStream = responseEntity.getContent();
        if (contentInputStream == null) {
            throw new Exception("No content stream for image: " + imageUrl.toString());
        }

        Bitmap bitmap = BitmapFactory.decodeStream(contentInputStream);
        ImageCache.saveImageInFileCache(imageKey, bitmap);
        LogWrapper.logMessage("Downloaded image: " + imageUrl.toString());
        bitmap.recycle();
    } catch (IOException e) {
        LogWrapper.logException(e);
        return false;
    } catch (Exception e) {
        LogWrapper.logException(e);
        return false;
    } finally {
        try {
            if (contentInputStream != null) {
                contentInputStream.close();
            }
            if (responseEntity != null) {
                responseEntity.consumeContent();
            }
        } catch (IOException e) {
            LogWrapper.logException(e);
        }
    }

    return true;
}

From source file:Main.java

public static Bitmap cutBitmap(int newWidth, int newHeight, Bitmap bitmap) {
    if (bitmap == null) {
        return null;
    }/*from  ww w  .j  a  v a  2 s .c o  m*/
    if (bitmap.getWidth() > newWidth) {
        int startX = (bitmap.getWidth() - newWidth) / 2;
        Bitmap targetBitmap = Bitmap.createBitmap(bitmap, startX, 0, newWidth, newHeight);
        if (!bitmap.isRecycled()) {
            bitmap.recycle();
        }
        return targetBitmap;
    } else if (bitmap.getHeight() > newHeight) {
        int startY = (bitmap.getHeight() - newHeight) / 2;
        Bitmap targetBitmap = Bitmap.createBitmap(bitmap, 0, startY, newWidth, newHeight);
        if (!bitmap.isRecycled()) {
            bitmap.recycle();
        }
        return targetBitmap;
    }
    return bitmap;
}