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 Bitmap rotateImage(Bitmap bitmap, float degrees) {
    Bitmap resultBitmap = bitmap;//from www . j a  v  a  2s  .c  om
    try {
        Matrix matrix = new Matrix();
        matrix.postRotate(degrees);
        // Rotate the bitmap
        resultBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    } catch (Exception exception) {
        exception.printStackTrace();
    }
    if (resultBitmap != bitmap) {
        bitmap.recycle();
    }
    return resultBitmap;
}

From source file:Main.java

/**
 * Loads the texture with the given id.// ww  w  . j a v  a2s  . co m
 *
 * @param context    the application's context
 * @param resourceId the id of the texture
 * @return the texture handle
 */
public static int loadTexture(final Context context, final int resourceId) {
    final int[] textureHandle = new int[1];

    if (!cachedTextureHandles.containsKey(resourceId)) {
        GLES20.glGenTextures(1, textureHandle, 0);

        if (textureHandle[0] != 0) {
            final BitmapFactory.Options options = new BitmapFactory.Options();
            options.inScaled = false; // Pre scaling off

            final Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resourceId);

            GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureHandle[0]);
            GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_REPEAT);
            GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_REPEAT);

            GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);

            bitmap.recycle();

            if (textureHandle[0] == 0) {
                throw new RuntimeException("Error loading texture.");
            }

            cachedTextureHandles.put(resourceId, textureHandle[0]);
        }
    }
    return cachedTextureHandles.get(resourceId);
}

From source file:Main.java

public static Bitmap forceConfig565(Bitmap original) {
    Bitmap convertedBitmap = original;// ww  w  .  j a  v  a2 s .c om
    if (original.getConfig() != Bitmap.Config.RGB_565) {
        convertedBitmap = Bitmap.createBitmap(original.getWidth(), original.getHeight(), Bitmap.Config.RGB_565);
        Canvas canvas = new Canvas(convertedBitmap);
        Paint paint = new Paint();
        paint.setColor(Color.BLACK);
        canvas.drawBitmap(original, 0, 0, paint);

        if (convertedBitmap != original) {
            original.recycle();
        }
    }

    return convertedBitmap;
}

From source file:Main.java

public static void saveBitmap(Bitmap bitmap, String descPath) {
    File file = new File(descPath);
    if (!file.getParentFile().exists()) {
        file.getParentFile().mkdirs();//from ww  w .j av  a 2 s  .  c  om
    }
    if (!file.exists()) {
        try {
            bitmap.compress(CompressFormat.JPEG, 30, new FileOutputStream(file));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

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

From source file:Main.java

public static Bitmap forceEvenBitmapSize(Bitmap original) {
    int width = original.getWidth();
    int height = original.getHeight();

    if (width % 2 == 1) {
        width++;/*  ww w .  j  a v a 2s.c om*/
    }
    if (height % 2 == 1) {
        height++;
    }

    Bitmap fixedBitmap = original;
    if (width != original.getWidth() || height != original.getHeight()) {
        fixedBitmap = Bitmap.createScaledBitmap(original, width, height, false);
    }

    if (fixedBitmap != original) {
        original.recycle();
    }

    return fixedBitmap;
}

From source file:Main.java

public static Bitmap rotate(Bitmap b, int degrees) {
    if (degrees != 0 && b != null) {
        Matrix m = new Matrix();
        m.setRotate(degrees, (float) b.getWidth() / 2, (float) b.getHeight() / 2);
        try {/*from   w ww  .j av  a  2s  .  co m*/
            Bitmap b2 = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), m, true);
            if (b != b2) {
                b.recycle();
                b = b2;
            }
        } catch (OutOfMemoryError ex) {
            // We have no memory to rotate. Return the original bitmap.
        }
    }
    return b;
}

From source file:com.entertailion.android.launcher.spotlight.ProcessSpotlight.java

/**
 * Process the spotlight JSON data/* w  ww .  j av a 2 s  . co  m*/
 * 
 * @param jsonFeed
 * @return
 * @throws JSONException
 */
public static List<SpotlightInfo> process(Context context, String jsonFeed) throws JSONException {
    JSONObject jsonObj = new JSONObject(jsonFeed);
    List<SpotlightInfo> result = null;
    if (null != jsonObj) {
        JSONArray models = (JSONArray) jsonObj.get("models");
        if (null != models && models.length() > 0) {
            result = new ArrayList<SpotlightInfo>();
            int position = 0;
            for (int i = 0; i < models.length(); i++) {
                JSONObject model = (JSONObject) models.get(i);
                String title = model.getString("title");
                String url = model.getString("url");
                String logo = model.getString("hdpiLogo");
                String icon = null;
                if (null == SpotlightInfo.spotlightIconMap.get(title.toLowerCase())) {
                    // icon file path
                    try {
                        icon = "spotlight_" + sanitizeName(title) + ".png";
                        File file = context.getFileStreamPath(icon);
                        if (!file.exists()) {
                            Uri uri = Uri.parse(url);
                            String alternateLogo = Utils.getWebSiteIcon(context,
                                    uri.getScheme() + "://" + uri.getHost());
                            if (alternateLogo != null && alternateLogo.trim().length() > 0) {
                                icon = alternateLogo;
                            } else {
                                // create a file-based icon from original
                                // 360x203
                                Bitmap bitmap = Utils.getBitmapFromURL(logo);
                                if (bitmap != null) {
                                    bitmap = Utils.crop(bitmap, 30, 30);
                                    Utils.saveToFile(context, bitmap, 100, 100, icon);
                                    bitmap.recycle();
                                    bitmap = null;
                                } else {
                                    icon = null;
                                }
                            }
                        }
                    } catch (Exception e) {
                        Log.e(LOG_TAG, "create spotlight icon", e);
                    }
                }
                Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                result.add(new SpotlightInfo(position++, title, browserIntent, logo, icon));
            }
        }
    }
    return result;
}

From source file:Main.java

public static boolean scaleImage(String origin_path, String result_path, int target_size) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;/*from   www  . j  av a 2 s  .c  om*/
    BitmapFactory.decodeFile(origin_path, options);
    int imageHeight = options.outHeight;
    int imageWidth = options.outWidth;
    int scale = (int) (Math.sqrt(imageWidth * imageHeight * 4 / target_size)) + 1;
    Bitmap bmp = null;
    options.inJustDecodeBounds = false;
    options.inSampleSize = scale;
    bmp = BitmapFactory.decodeFile(origin_path, options);

    FileOutputStream out = null;
    try {
        out = new FileOutputStream(result_path);
        bmp.compress(Bitmap.CompressFormat.PNG, 100, out);
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    } finally {
        try {
            bmp.recycle();
            if (out != null) {
                out.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

public static Bitmap getDesirableBitmap(String imgPath, int desiredSize) {
    try {/*from   www  .  j a  va2 s.  c o m*/
        int scale = getDesirableBitmapSampleSize(imgPath, desiredSize);
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = scale;
        Bitmap originalBitmap = BitmapFactory.decodeFile(imgPath, options);

        int rotation = getExifOrientation(imgPath);
        if (rotation != 0) {
            Matrix matrix = new Matrix();
            matrix.postRotate(rotation);
            Bitmap exifBitmap = Bitmap.createBitmap(originalBitmap, 0, 0, originalBitmap.getWidth(),
                    originalBitmap.getHeight(), matrix, true);
            originalBitmap.recycle();
            return exifBitmap;
        }
        return originalBitmap;
    } catch (Exception ex) {
        ex.printStackTrace();
        //TODO: Handle
    } catch (OutOfMemoryError e) {
        e.printStackTrace();
        //Hmmm...what to do...
    }
    return null;
}

From source file:Main.java

public static Bitmap makeCircleBitmap(Bitmap original) {
    final int width = original.getWidth();
    final int height = original.getHeight();
    final float radius = Math.min(width, height) / 2;

    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

    Paint paint = new Paint();
    paint.setAntiAlias(true);/* w w  w .java2 s.c  o m*/
    paint.setShader(new BitmapShader(original, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));

    Canvas canvas = new Canvas(bitmap);
    canvas.drawCircle(radius, radius, radius, paint);

    original.recycle();
    return bitmap;
}