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 static int initTexture(Context context, int drawableId)// textureId
{
    int[] textures = new int[1];
    glGenTextures(1, textures, 0);//  w  w w.  j  av a 2s. c  o  m
    int textureId = textures[0];
    glBindTexture(GL_TEXTURE_2D, textureId);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

    InputStream is = context.getResources().openRawResource(drawableId);
    Bitmap bitmapTmp;
    try {
        bitmapTmp = BitmapFactory.decodeStream(is);
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    GLUtils.texImage2D(GL_TEXTURE_2D, 0, bitmapTmp, 0);
    bitmapTmp.recycle();

    return textureId;
}

From source file:Main.java

public static Bitmap rotateBitmap(int degrees, Bitmap bitmap) {
    if (degrees == 0 || null == bitmap) {
        return bitmap;
    }/*from   w  w  w .j  ava2  s  .  co  m*/
    Matrix matrix = new Matrix();
    matrix.setRotate(degrees, bitmap.getWidth() / 2, bitmap.getHeight() / 2);
    Bitmap bmp = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    if (null != bitmap) {
        bitmap.recycle();
        bitmap = null;
    }
    return bmp;
}

From source file:io.card.development.recording.ManifestEntry.java

private static byte[] decompress(byte[] compressed) {
    /*//from  ww w . j ava2  s.  c  o m
     * DO NOT EVER USE THIS METHOD IN PRODUCTION This is horribly inefficient, but written only
     * for testing purposes.
     */

    Bitmap b = BitmapFactory.decodeByteArray(compressed, 0, compressed.length);
    ByteBuffer bb = ByteBuffer.allocate(b.getWidth() * b.getHeight() * 4);
    b.copyPixelsToBuffer(bb);
    b.recycle();

    byte[] ba = bb.array();
    byte[] singleChannel = new byte[ba.length / 4]; // 4 channels
    for (int i = 0; i < singleChannel.length; i++) {
        singleChannel[i] = ba[i * 4 + 1];
    }

    return singleChannel;
}

From source file:Main.java

public static Bitmap rotateBitmap(Bitmap source, int rotation, boolean recycle) {
    if (rotation == 0)
        return source;
    int w = source.getWidth();
    int h = source.getHeight();
    Matrix m = new Matrix();
    m.postRotate(rotation);/*from w ww  . jav a2 s  . co  m*/
    Bitmap bitmap = Bitmap.createBitmap(source, 0, 0, w, h, m, true);
    if (recycle)
        source.recycle();
    return bitmap;
}

From source file:Main.java

public static Bitmap decodeScaleImage(String path, int targetWidth, int targetHeight) {
    BitmapFactory.Options bitmapOptions = getBitmapOptions(path);
    bitmapOptions.inSampleSize = calculateInSampleSize(bitmapOptions, targetWidth, targetHeight);
    bitmapOptions.inJustDecodeBounds = false;
    Bitmap noRotatingBitmap = BitmapFactory.decodeFile(path, bitmapOptions);
    int degree = readPictureDegree(path);
    Bitmap rotatingBitmap;// w ww  .  j a v  a  2s  .  c o  m
    if (noRotatingBitmap != null && degree != 0) {
        rotatingBitmap = rotatingImageView(degree, noRotatingBitmap);
        noRotatingBitmap.recycle();
        return rotatingBitmap;
    } else {
        return noRotatingBitmap;
    }
}

From source file:Main.java

public static int loadTexture(final Context context, final int resourceId, int[] size) {
    final int texId = genTexture();

    if (texId != 0) {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inScaled = false; // No pre-scaling
        options.inJustDecodeBounds = true;

        // Just decode bounds
        BitmapFactory.decodeResource(context.getResources(), resourceId, options);

        // Set return size
        size[0] = options.outWidth;/*from  w  w  w  .j a  v  a 2  s . c  o  m*/
        size[1] = options.outHeight;

        // Decode
        options.inJustDecodeBounds = false;
        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resourceId, options);

        // Load the bitmap into the bound texture.
        GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);

        // Recycle the bitmap, since its data has been loaded into OpenGL.
        bitmap.recycle();
    }

    return texId;
}

From source file:Main.java

protected static Uri scaleDown(Uri imageUri, int targetSize, Context context) {
    System.gc();/* www .j a  v  a2  s  . co m*/
    String imagePath = getImagePath(imageUri, context);
    Bitmap currentImage = BitmapFactory.decodeFile(imagePath);

    int targetWidth = targetSize;
    int targetHeight = targetSize;

    int width = currentImage.getWidth();
    int height = currentImage.getHeight();

    if (width < targetWidth || height < targetHeight) {
        currentImage.recycle();
        currentImage = null;
        System.gc();
        return Uri.parse(imageUri.toString());
    }

    height = (int) (height * (float) targetWidth / width);

    Bitmap scaledBitmap = Bitmap.createScaledBitmap(currentImage, targetWidth, height, false);

    if (currentImage != scaledBitmap) {
        currentImage.recycle();
        currentImage = null;
    }
    System.gc();
    File imageFile;
    try {
        imageFile = new File(context.getCacheDir(), "vumatch-upload-00.jpeg");
        FileOutputStream output;

        output = new FileOutputStream(imageFile);
        boolean result = scaledBitmap.compress(CompressFormat.JPEG, 90, output);
        if (result) {
            scaledBitmap.recycle();
            scaledBitmap = null;
            return Uri.fromFile(imageFile);
        } else {
            return null;
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:com.radadev.xkcd.Comics.java

public static void downloadComic(Integer comicNumber, Context context)
        throws FileNotFoundException, IOException {
    ComicDbAdapter dbAdapter = new ComicDbAdapter(context);
    dbAdapter.open();/*from w w w . j  a va  2s.c  o m*/
    try {
        dbAdapter.updateComic(comicNumber);
        File file = new File(getSdDir(context), comicNumber.toString() + ".png");
        if (file.length() <= 0) {
            Cursor cursor = dbAdapter.fetchComic(comicNumber);
            String url = cursor.getString(cursor.getColumnIndexOrThrow(Comics.SQL_KEY_IMAGE));

            if (url == null || url.length() == 0) {
                dbAdapter.updateComic(comicNumber);
                cursor.close();
                cursor = dbAdapter.fetchComic(comicNumber);
                url = cursor.getString(cursor.getColumnIndexOrThrow(Comics.SQL_KEY_IMAGE));
            }
            cursor.close();

            Bitmap bitmap = Comics.downloadBitmap(url);
            FileOutputStream fileStream = new FileOutputStream(file);
            bitmap.compress(CompressFormat.PNG, 100, fileStream);
            bitmap.recycle();
            fileStream.close();
        }
    } finally {
        dbAdapter.close();
    }
}

From source file:Main.java

public static Bitmap ScaleAndRotateBitmap(Bitmap bitmap, int rotation, int flipHorizontal) {
    Matrix m = new Matrix();
    if (flipHorizontal != 0) {
        m.postScale(-1, 1);/*  ww  w .j  av a 2  s  . co m*/
    }
    if (rotation != 0) {
        m.postRotate(rotation);
    }
    Bitmap finalBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m, true);
    if (finalBitmap != bitmap) {
        bitmap.recycle();
    }
    return finalBitmap;
}

From source file:Main.java

public static Bitmap decodeBitmap(File file, int dstWidth, int dstHeight) {
    // First decode with inJustDecodeBounds=true to check dimensions
    final Options options = new Options();
    options.inScaled = false;//from   w  w w . ja v a  2s  . co m
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(file.getAbsolutePath(), options);

    // Decode the bitmap with inSampleSize set
    options.inSampleSize = calculateBitmapRatio(options, dstWidth, dstHeight);
    options.inJustDecodeBounds = false;
    Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath(), options);
    if (bitmap == null) {
        return null;
    }

    // Test if the bitmap has exif format, and decode properly
    Bitmap out = decodeExifBitmap(file, bitmap);
    if (!out.equals(bitmap)) {
        bitmap.recycle();
    }
    return out;
}