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:mil.nga.giat.mage.sdk.utils.MediaUtility.java

public static Bitmap resizeAndRoundCorners(Bitmap bitmap, int maxSize) {
    boolean isLandscape = bitmap.getWidth() > bitmap.getHeight();

    int newWidth, newHeight;
    if (isLandscape) {
        newWidth = maxSize;/*from  w w  w . j  ava  2 s  .c om*/
        newHeight = Math.round(((float) newWidth / bitmap.getWidth()) * bitmap.getHeight());
    } else {
        newHeight = maxSize;
        newWidth = Math.round(((float) newHeight / bitmap.getHeight()) * bitmap.getWidth());
    }

    Bitmap resizedBitmap = Bitmap.createScaledBitmap(bitmap, newWidth, newHeight, false);

    if (resizedBitmap != bitmap)
        bitmap.recycle();

    Bitmap roundedProfile = Bitmap.createBitmap(resizedBitmap.getWidth(), resizedBitmap.getHeight(),
            Config.ARGB_8888);

    Canvas roundedCanvas = new Canvas(roundedProfile);
    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, roundedProfile.getWidth(), roundedProfile.getHeight());
    final RectF rectF = new RectF(rect);
    final float roundPx = 7.0f;

    paint.setAntiAlias(true);
    roundedCanvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    roundedCanvas.drawRoundRect(rectF, roundPx, roundPx, paint);

    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    roundedCanvas.drawBitmap(resizedBitmap, rect, rect, paint);
    return roundedProfile;
}

From source file:com.plugin.gallery.ForegroundGalleryLauncher.java

/**
 * Called when the camera view exits.//w w w .  ja  v a2 s  . co m
 * 
 * @param requestCode
 *            The request code originally supplied to
 *            startActivityForResult(), allowing you to identify who this
 *            result came from.
 * @param resultCode
 *            The integer result code returned by the child activity through
 *            its setResult().
 * @param intent
 *            An Intent, which can return result data to the caller (various
 *            data can be attached to Intent "extras").
 */
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {

    if (resultCode == Activity.RESULT_OK) {

        Uri uri = intent.getData();
        String fileURL = intent.getStringExtra("fileURL");
        System.out.println("fileURL = " + fileURL);
        ContentResolver resolver = this.cordova.getActivity().getContentResolver();

        try {
            Bitmap bitmap = android.graphics.BitmapFactory.decodeStream(resolver.openInputStream(uri));
            bitmap = scaleBitmap(bitmap);
            this.processPicture(bitmap);
            bitmap.recycle();
            bitmap = null;
            this.callbackContext.success("" + fileURL);
            System.gc();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            this.failPicture("Error retrieving image.");
        }
    } else if (resultCode == Activity.RESULT_CANCELED) {
        this.failPicture("Selection cancelled.");
    } else {
        this.failPicture("Selection did not complete!");
    }
}

From source file:Main.java

public static boolean saveMyBitmap(File f, Bitmap mBitmap) throws IOException {
    boolean saveComplete = true;
    try {//from ww  w .j  a  v  a 2  s. co  m
        f.createNewFile();
        FileOutputStream fOut = null;
        fOut = new FileOutputStream(f);
        int width = mBitmap.getWidth();
        int height = mBitmap.getHeight();
        int finalWidth = 800;
        int finalHeight = (int) (finalWidth * 1.0 * (height * 1.0 / width * 1.0));
        double x = width * finalHeight;
        double y = height * finalWidth;

        if (x > y) {
            finalHeight = (int) (y / (double) width);
        } else if (x < y) {
            finalWidth = (int) (x / (double) height);
        }

        if (finalWidth > width && finalHeight > height) {
            finalWidth = width;
            finalHeight = height;
        }
        Matrix matrix = new Matrix();
        matrix.reset();
        float scaleWidth = ((float) finalWidth) / (float) width;
        float scaleHeight = ((float) finalHeight) / (float) height;
        matrix.postScale(scaleWidth, scaleHeight);
        mBitmap = Bitmap.createBitmap(mBitmap, 0, 0, (int) width, (int) height, matrix, true);
        mBitmap.compress(Bitmap.CompressFormat.JPEG, 80, fOut);
        fOut.flush();
        fOut.close();
        mBitmap.recycle();
        System.gc();
    } catch (FileNotFoundException e) {
        saveComplete = false;
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
        saveComplete = false;
    }
    return saveComplete;
}

From source file:biz.varkon.shelvesom.provider.toys.ToysUpdater.java

public void run() {
    Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
    final ImageUtilities.ExpiringBitmap expiring = new ImageUtilities.ExpiringBitmap();

    while (!mStopped) {
        try {//from w  w w .j a v a2 s. co  m
            final String toyId = mQueue.take();

            final Long lastCheck = sLastChecks.get(toyId);
            if (lastCheck != null && (lastCheck + ONE_DAY) >= System.currentTimeMillis()) {
                continue;
            }
            sLastChecks.put(toyId, System.currentTimeMillis());

            final ToysStore.Toy toy = ToysManager.findToy(mResolver, toyId, null);

            if (toy == null)
                continue;

            final String imgURL = Preferences.getImageURLForUpdater(toy);

            if (toy.getLastModified() == null || imgURL == null) {
                continue;
            }

            if (toyCoverUpdated(toy, expiring) && expiring.lastModified != null) {
                ImageUtilities.deleteCachedCover(toyId);

                final Bitmap bitmap = Preferences.getBitmapForManager(toy);

                ImportUtilities.addCoverToCache(toy.getInternalId(), bitmap);

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

                mValues.put(BaseItem.LAST_MODIFIED, expiring.lastModified.getTimeInMillis());
                mArguments[0] = toyId;
                mResolver.update(ToysStore.Toy.CONTENT_URI, mValues, mSelection, mArguments);
            }

            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // Ignore
        }
    }
}

From source file:biz.varkon.shelvesom.provider.tools.ToolsUpdater.java

public void run() {
    Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
    final ImageUtilities.ExpiringBitmap expiring = new ImageUtilities.ExpiringBitmap();

    while (!mStopped) {
        try {//from w ww .j a va2s .  c o  m
            final String toolId = mQueue.take();

            final Long lastCheck = sLastChecks.get(toolId);
            if (lastCheck != null && (lastCheck + ONE_DAY) >= System.currentTimeMillis()) {
                continue;
            }
            sLastChecks.put(toolId, System.currentTimeMillis());

            final ToolsStore.Tool tool = ToolsManager.findTool(mResolver, toolId, null);

            if (tool == null)
                continue;

            final String imgURL = Preferences.getImageURLForUpdater(tool);

            if (tool.getLastModified() == null || imgURL == null) {
                continue;
            }

            if (toolCoverUpdated(tool, expiring) && expiring.lastModified != null) {
                ImageUtilities.deleteCachedCover(toolId);

                final Bitmap bitmap = Preferences.getBitmapForManager(tool);

                ImportUtilities.addCoverToCache(tool.getInternalId(), bitmap);

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

                mValues.put(BaseItem.LAST_MODIFIED, expiring.lastModified.getTimeInMillis());
                mArguments[0] = toolId;
                mResolver.update(ToolsStore.Tool.CONTENT_URI, mValues, mSelection, mArguments);
            }

            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // Ignore
        }
    }
}

From source file:biz.varkon.shelvesom.provider.books.BooksUpdater.java

public void run() {
    Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
    final ImageUtilities.ExpiringBitmap expiring = new ImageUtilities.ExpiringBitmap();

    while (!mStopped) {
        try {/*from  www .  j a va  2s . c o  m*/
            final String bookId = mQueue.take();

            final Long lastCheck = sLastChecks.get(bookId);
            if (lastCheck != null && (lastCheck + ONE_DAY) >= System.currentTimeMillis()) {
                continue;
            }
            sLastChecks.put(bookId, System.currentTimeMillis());

            final BooksStore.Book book = BooksManager.findBook(mResolver, bookId, null);

            if (book == null)
                continue;

            final String imgURL = Preferences.getImageURLForUpdater(book);

            if (book.getLastModified() == null || imgURL == null) {
                continue;
            }

            if (bookCoverUpdated(book, expiring) && expiring.lastModified != null) {
                ImageUtilities.deleteCachedCover(bookId);

                final Bitmap bitmap = Preferences.getBitmapForManager(book);

                ImportUtilities.addCoverToCache(book.getInternalId(), bitmap);

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

                mValues.put(BaseItem.LAST_MODIFIED, expiring.lastModified.getTimeInMillis());
                mArguments[0] = bookId;
                mResolver.update(BooksStore.Book.CONTENT_URI, mValues, mSelection, mArguments);
            }

            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // Ignore
        }
    }
}

From source file:biz.varkon.shelvesom.provider.music.MusicUpdater.java

public void run() {
    Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
    final ImageUtilities.ExpiringBitmap expiring = new ImageUtilities.ExpiringBitmap();

    while (!mStopped) {
        try {//from w w  w  .ja  v a 2s . c o  m
            final String musicId = mQueue.take();

            final Long lastCheck = sLastChecks.get(musicId);
            if (lastCheck != null && (lastCheck + ONE_DAY) >= System.currentTimeMillis()) {
                continue;
            }
            sLastChecks.put(musicId, System.currentTimeMillis());

            final MusicStore.Music music = MusicManager.findMusic(mResolver, musicId, null);

            if (music == null)
                continue;

            final String imgURL = Preferences.getImageURLForUpdater(music);

            if (music.getLastModified() == null || imgURL == null) {
                continue;
            }

            if (musicCoverUpdated(music, expiring) && expiring.lastModified != null) {
                ImageUtilities.deleteCachedCover(musicId);

                final Bitmap bitmap = Preferences.getBitmapForManager(music);

                ImportUtilities.addCoverToCache(music.getInternalId(), bitmap);

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

                mValues.put(BaseItem.LAST_MODIFIED, expiring.lastModified.getTimeInMillis());
                mArguments[0] = musicId;
                mResolver.update(MusicStore.Music.CONTENT_URI, mValues, mSelection, mArguments);
            }

            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // Ignore
        }
    }
}

From source file:biz.varkon.shelvesom.provider.comics.ComicsUpdater.java

public void run() {
    Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
    final ImageUtilities.ExpiringBitmap expiring = new ImageUtilities.ExpiringBitmap();

    while (!mStopped) {
        try {/*  w w w  .ja v a  2s .  c o  m*/
            final String comicId = mQueue.take();

            final Long lastCheck = sLastChecks.get(comicId);
            if (lastCheck != null && (lastCheck + ONE_DAY) >= System.currentTimeMillis()) {
                continue;
            }
            sLastChecks.put(comicId, System.currentTimeMillis());

            final ComicsStore.Comic comic = ComicsManager.findComic(mResolver, comicId, null);

            if (comic == null)
                continue;

            final String imgURL = Preferences.getImageURLForUpdater(comic);

            if (comic.getLastModified() == null || imgURL == null) {
                continue;
            }

            if (comicCoverUpdated(comic, expiring) && expiring.lastModified != null) {
                ImageUtilities.deleteCachedCover(comicId);

                final Bitmap bitmap = Preferences.getBitmapForManager(comic);

                ImportUtilities.addCoverToCache(comic.getInternalId(), bitmap);

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

                mValues.put(BaseItem.LAST_MODIFIED, expiring.lastModified.getTimeInMillis());
                mArguments[0] = comicId;
                mResolver.update(ComicsStore.Comic.CONTENT_URI, mValues, mSelection, mArguments);
            }

            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // Ignore
        }
    }
}

From source file:biz.varkon.shelvesom.provider.movies.MoviesUpdater.java

public void run() {
    Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
    final ImageUtilities.ExpiringBitmap expiring = new ImageUtilities.ExpiringBitmap();

    while (!mStopped) {
        try {/*from   ww  w .  j ava  2 s. c  om*/
            final String movieId = mQueue.take();

            final Long lastCheck = sLastChecks.get(movieId);
            if (lastCheck != null && (lastCheck + ONE_DAY) >= System.currentTimeMillis()) {
                continue;
            }
            sLastChecks.put(movieId, System.currentTimeMillis());

            final MoviesStore.Movie movie = MoviesManager.findMovie(mResolver, movieId, null);

            if (movie == null)
                continue;

            final String imgURL = Preferences.getImageURLForUpdater(movie);

            if (movie.getLastModified() == null || imgURL == null) {
                continue;
            }

            if (movieCoverUpdated(movie, expiring) && expiring.lastModified != null) {
                ImageUtilities.deleteCachedCover(movieId);

                final Bitmap bitmap = Preferences.getBitmapForManager(movie);

                ImportUtilities.addCoverToCache(movie.getInternalId(), bitmap);

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

                mValues.put(BaseItem.LAST_MODIFIED, expiring.lastModified.getTimeInMillis());
                mArguments[0] = movieId;
                mResolver.update(MoviesStore.Movie.CONTENT_URI, mValues, mSelection, mArguments);
            }

            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // Ignore
        }
    }
}

From source file:biz.varkon.shelvesom.provider.gadgets.GadgetsUpdater.java

public void run() {
    Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
    final ImageUtilities.ExpiringBitmap expiring = new ImageUtilities.ExpiringBitmap();

    while (!mStopped) {
        try {/*w  w w  .  j av  a2  s .com*/
            final String gadgetId = mQueue.take();

            final Long lastCheck = sLastChecks.get(gadgetId);
            if (lastCheck != null && (lastCheck + ONE_DAY) >= System.currentTimeMillis()) {
                continue;
            }
            sLastChecks.put(gadgetId, System.currentTimeMillis());

            final GadgetsStore.Gadget gadget = GadgetsManager.findGadget(mResolver, gadgetId, null);

            if (gadget == null)
                continue;

            final String imgURL = Preferences.getImageURLForUpdater(gadget);

            if (gadget.getLastModified() == null || imgURL == null) {
                continue;
            }

            if (gadgetCoverUpdated(gadget, expiring) && expiring.lastModified != null) {
                ImageUtilities.deleteCachedCover(gadgetId);

                final Bitmap bitmap = Preferences.getBitmapForManager(gadget);

                ImportUtilities.addCoverToCache(gadget.getInternalId(), bitmap);

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

                mValues.put(BaseItem.LAST_MODIFIED, expiring.lastModified.getTimeInMillis());
                mArguments[0] = gadgetId;
                mResolver.update(GadgetsStore.Gadget.CONTENT_URI, mValues, mSelection, mArguments);
            }

            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // Ignore
        }
    }
}