Example usage for android.graphics Bitmap getRowBytes

List of usage examples for android.graphics Bitmap getRowBytes

Introduction

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

Prototype

public final int getRowBytes() 

Source Link

Document

Return the number of bytes between rows in the bitmap's pixels.

Usage

From source file:com.crossbow.volley.toolbox.CrossbowImageCache.java

/**
 * @param size - number of byes to use for the cache;
 *//*from w ww . j av  a 2  s.  com*/
protected CrossbowImageCache(int size) {
    imageCache = new LruCache<String, Bitmap>(size) {
        @Override
        protected int sizeOf(String key, Bitmap value) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                return value.getAllocationByteCount();
            } else {
                return value.getRowBytes() * value.getHeight();
            }
        }
    };
}

From source file:edu.purdue.safewalk.bitmaps.BitmapHelper.java

public void initMemoryCache() {
    final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);

    // Use 1/8th of the available memory for this memory cache.
    final int cacheSize = maxMemory / 15;

    mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
        @Override/* w w  w . j  av a 2 s . c om*/
        protected int sizeOf(String key, Bitmap bitmap) {
            // The cache size will be measured in kilobytes rather than
            // number of items.
            return (bitmap.getRowBytes() * bitmap.getHeight()) / 1024;
        }
    };
}

From source file:org.opensilk.music.artwork.cache.BitmapLruCache.java

@Override
@TargetApi(Build.VERSION_CODES.KITKAT)/*from w  ww .  ja v  a2 s .co m*/
protected int sizeOf(String key, Bitmap value) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        return value.getAllocationByteCount();
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) {
        return value.getByteCount();
    } else {
        return value.getRowBytes() * value.getHeight();
    }
}

From source file:org.jitsi.android.gui.util.DrawableCache.java

/**
 * Creates new instance of <tt>DrawableCache</tt>.
 *//*from   w  w  w  .  ja  v  a 2s. c  o  m*/
public DrawableCache() {
    // Get max available VM memory, exceeding this amount will throw an
    // OutOfMemory exception. Stored in kilobytes as LruCache takes an
    // int in its constructor.
    final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);

    // Use 1/8th of the available memory for this memory cache.
    final int cacheSize = maxMemory / 8;

    cache = new LruCache<String, BitmapDrawable>(cacheSize) {
        @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
        @Override
        protected int sizeOf(String key, BitmapDrawable value) {
            Bitmap bmp = value.getBitmap();
            int byteSize;
            if (AndroidUtils.hasAPI(Build.VERSION_CODES.HONEYCOMB_MR1)) {
                byteSize = bmp.getByteCount();
            } else {
                byteSize = bmp.getRowBytes() * bmp.getHeight();
            }
            return byteSize / 1024;
        }
    };
}

From source file:Main.java

public static Bitmap convertToMutable(Bitmap srcBitmap, String cacheDirPath, String tempFileName) {
    try {//from  w  w  w .  j a  v  a 2s. co m
        // this is the file going to use temporally to save the bytes.
        // This file will not be a image, it will store the raw image data.
        int index = tempFileName.lastIndexOf(".");
        if (index != -1)
            tempFileName = tempFileName.substring(0, index);
        File file = new File(cacheDirPath + File.separator + tempFileName + ".tmp");

        // Open an RandomAccessFile
        // Make sure you have added uses-permission
        // android:name="android.permission.WRITE_EXTERNAL_STORAGE"
        // into AndroidManifest.xml file
        RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");

        // get the width and height of the source bitmap.
        int width = srcBitmap.getWidth();
        int height = srcBitmap.getHeight();
        Config type = srcBitmap.getConfig();

        // Copy the byte to the file
        // Assume source bitmap loaded using options.inPreferredConfig =
        // Config.ARGB_8888;
        FileChannel channel = randomAccessFile.getChannel();
        MappedByteBuffer map = channel.map(MapMode.READ_WRITE, 0, srcBitmap.getRowBytes() * height);
        srcBitmap.copyPixelsToBuffer(map);
        // recycle the source bitmap, this will be no longer used.
        srcBitmap.recycle();
        System.gc();// try to force the bytes from the imgIn to be released

        // Create a new bitmap to load the bitmap again. Probably the memory
        // will be available.
        srcBitmap = Bitmap.createBitmap(width, height, type);
        map.position(0);
        // load it back from temporary
        srcBitmap.copyPixelsFromBuffer(map);
        // close the temporary file and channel , then delete that also
        channel.close();
        randomAccessFile.close();

        // delete the temp file
        file.delete();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return srcBitmap;
}

From source file:com.yanzhenjie.album.task.LocalImageLoader.java

public LocalImageLoader() {
    mExecutorService = Executors.newFixedThreadPool(6);

    int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 4);
    mLruCache = new LruCache<String, Bitmap>(maxMemory) {
        @Override//from w w w . ja  va  2s . c  om
        protected int sizeOf(String key, Bitmap value) {
            return value.getRowBytes() * value.getHeight();
        }
    };
}

From source file:de.hu_berlin.informatik.spws2014.mapever.largeimageview.CachedImage.java

/**
 * Gre eines Cache-Eintrags (Bitmap) in Kilobyte. Die Gre des Caches insgesamt wird also an der Menge der
 * Bitmapdaten statt an der Anzahl der Eintrge gemessen.
 *//* w  ww  .  j av  a 2  s .c om*/
@Override
protected int sizeOf(String key, Bitmap bitmap) {
    // (getByteCount() (API 12) == getRowBytes() * getHeight())
    return (bitmap.getRowBytes() * bitmap.getHeight()) / 1024;
}

From source file:com.blogspot.codigogoogle.listloadingsamples.ImageLoaderListAdapter.java

public ImageLoaderListAdapter(Activity context, ArrayList<Map<String, String>> listItems) {
    this.context = context;
    this.listItems = listItems;

    mPlaceHolderBitmap = decodeSampledBitmapFromResource(context.getResources(), R.drawable.img_placeholder, 20,
            20);//ww w.jav a  2 s.  c om

    final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
    final int cacheSize = maxMemory / 8;
    mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
        @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
        @Override
        protected int sizeOf(String key, Bitmap bitmap) {
            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB_MR1) {
                return bitmap.getByteCount() / 1024;
            } else {
                return bitmap.getRowBytes() * bitmap.getHeight() / 1024;
            }
        }
    };
}

From source file:uk.co.senab.bitmapcache.CacheableBitmapDrawable.java

CacheableBitmapDrawable(String url, Resources resources, Bitmap bitmap,
        BitmapLruCache.RecyclePolicy recyclePolicy, int source) {
    super(resources, bitmap);

    mMemorySize = null != bitmap ? (bitmap.getRowBytes() * bitmap.getHeight()) : 0;
    mUrl = url;//from ww w.  j  a v a  2s  .  c  om
    mRecyclePolicy = recyclePolicy;
    mDisplayingCount = 0;
    mCacheCount = 0;
    mSource = source;
}