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.raptureinvenice.webimageview.cache.WebImageCache.java

public WebImageCache() {
    mMemCache = new LruCache<String, Bitmap>(cacheSize) {
        @Override//from   ww w. j av a 2s.  c  o  m
        protected int sizeOf(String key, Bitmap bitmap) {
            return bitmap.getRowBytes() * bitmap.getHeight();
        }
    };
}

From source file:com.seun.gallery.util.ImageCache.java

/**
 * @param candidate     - Bitmap to check
 * @param targetOptions - Options that have the out* value populated
 * @return true if <code>candidate</code> can be used for inBitmap re-use with
 * <code>targetOptions</code>
 *//* w  w  w . j  a v a2 s  .  co m*/
private static boolean canUseForInBitmap(Bitmap candidate, BitmapFactory.Options targetOptions) {
    //BEGIN_INCLUDE(can_use_for_inbitmap)
    if (!(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)) {
        // On earlier versions, the dimensions must match exactly and the inSampleSize must be 1
        return candidate.getWidth() == targetOptions.outWidth
                && candidate.getHeight() == targetOptions.outHeight && targetOptions.inSampleSize == 1;
    }

    // From Android 4.4 (KitKat) onward we can re-use if the byte size of the new bitmap
    // is smaller than the reusable bitmap candidate allocation byte count.
    int width = targetOptions.outWidth / targetOptions.inSampleSize;
    int height = targetOptions.outHeight / targetOptions.inSampleSize;
    int byteCount = width * height * getBytesPerPixel(candidate.getConfig());
    return byteCount <= (candidate.getRowBytes() * candidate.getHeight());
    //END_INCLUDE(can_use_for_inbitmap)
}

From source file:org.mythdroid.cache.ImageCache.java

/**
 * Constructor/*from  w w w  .j a v a  2  s  .c  o  m*/
 * @param name name of the cache
 * @param memCapacity maximum capacity of MemCache in bytes
 * @param memMaxSize maximum size of image to cache in memory in bytes
 * @param diskMaxSize maximum size of disk backed cache in bytes
 */
public ImageCache(String name, long memCapacity, long memMaxSize, int diskMaxSize) {
    try {
        diskCache = new ImageDiskCache(name, diskMaxSize);
    } catch (IOException e) {
        LogUtil.debug("Disk cache disabled: " + e.getMessage()); //$NON-NLS-1$
    }

    if (diskCache != null && diskCacheThread == null)
        newDiskCacheThread();

    memCache = new LruCache<String, Bitmap>((int) memCapacity) {
        @Override
        protected int sizeOf(String key, Bitmap value) {
            return value.getRowBytes() * value.getHeight();
        }
    };

    memMax = memMaxSize;

}

From source file:com.zhongyun.viewer.utils.VideoListImage.java

public VideoListImage(Context context, int serverType) {
    this.context = context;
    this.media = Viewer.getViewer().getMedia();
    cacheDir = FileUtils.createFile(Constants.LOCAL_ICON_PATH);
    options.inJustDecodeBounds = false;/*w  ww .ja v  a 2s. com*/
    options.inPurgeable = true;

    int MAXMEMONRY = (int) (Runtime.getRuntime().maxMemory() / 1024);
    if (filesCache == null) {
        filesCache = new LruCache<String, Bitmap>(MAXMEMONRY / 8) {
            @Override
            protected int sizeOf(String key, Bitmap bitmap) {
                return bitmap.getRowBytes() * bitmap.getHeight() / 1024;
            }

            @Override
            protected void entryRemoved(boolean evicted, String key, Bitmap oldValue, Bitmap newValue) {
                if (oldValue != null && !oldValue.isRecycled()) {
                    oldValue.recycle();
                    oldValue = null;
                }
            }
        };
    }
}

From source file:net.evecom.androidecssp.activity.pub.imagescan.NativeImageLoader.java

/**
 * /*from  w w w. java 2s .  c o  m*/
 * 
 * 
 * @author Mars zhang
 * @created 2015-11-25 12:06:01
 */
private NativeImageLoader() {
    // 
    final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);

    // 1/4
    final int cacheSize = maxMemory / 4;
    mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {

        // 
        @Override
        protected int sizeOf(String key, Bitmap bitmap) {
            return bitmap.getRowBytes() * bitmap.getHeight() / 1024;
        }
    };
}

From source file:com.extradea.framework.images.cache.LruBitmapCache.java

public LruBitmapCache(int size) {
    imageTempCache = new HashMap<String, WeakReference<Bitmap>>();
    imageCache = new LruCache<String, Bitmap>(size) {
        @Override/*from w ww .  j  a  v  a  2s  .  c  o m*/
        protected int sizeOf(String key, Bitmap value) {
            if (Build.VERSION.SDK_INT >= 12) {
                return value.getByteCount();
            } else {
                return value.getRowBytes() * value.getHeight();
            }
        }
    };
}

From source file:com.dv.BitMap.Native.DvNativeImageLoader.java

private DvNativeImageLoader() {

    // ??/*from   ww  w.  ja v  a  2s . c o m*/
    final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);

    // 1/4?
    final int cacheSize = maxMemory / 4;

    mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {

        // ???
        @Override
        protected int sizeOf(String key, Bitmap bitmap) {
            return bitmap.getRowBytes() * bitmap.getHeight() / 1024;
        }
    };
}

From source file:com.zlk.bigdemo.freeza.image.cache.MagicImageCache.java

/**
 * Initialize the cache, providing all parameters.
 * /*from w  w  w. ja  v a 2  s.  c  o  m*/
 * @param context
 *            The context to use
 * @param cacheParams
 *            The cache parameters to initialize the cache
 */
private void init(Context context, ImageCacheParams cacheParams) {

    // Set up memory cache
    if (cacheParams.memoryCacheEnabled) {
        mMemoryCache = new LruCache<String, Bitmap>(cacheParams.memCacheSize) {
            /**
             * Measure item size in bytes rather than units which is more
             * practical for a bitmap cache
             */
            @Override
            protected int sizeOf(String key, Bitmap bitmap) {
                int size = bitmap.getRowBytes() * bitmap.getHeight();
                // WxLog.d("setting", "Bitmap size:" + size);
                return size;
            }
        };
    }
}

From source file:org.matrix.androidsdk.db.MXMediaWorkerTask.java

/**
 * Search a cached bitmap from an url./*from  ww  w .  ja va2 s. c  om*/
 * rotationAngle is set to Integer.MAX_VALUE when undefined : the EXIF metadata must be checked.
 *
 * @param baseFile the base file
 * @param url the media url
 * @param rotation the bitmap rotation
 * @param mimeType the mime type
 * @return the cached bitmap or null it does not exist
 */
public static Bitmap bitmapForURL(Context context, File baseFile, String url, int rotation, String mimeType) {
    Bitmap bitmap = null;

    // sanity check
    if (null != url) {

        if (null == sMemoryCache) {
            int lruSize = Math.min(20 * 1024 * 1024, (int) Runtime.getRuntime().maxMemory() / 8);

            Log.d(LOG_TAG, "bitmapForURL  lruSize : " + lruSize);

            sMemoryCache = new LruCache<String, Bitmap>(lruSize) {
                @Override
                protected int sizeOf(String key, Bitmap bitmap) {
                    return bitmap.getRowBytes() * bitmap.getHeight(); // size in bytes
                }
            };
        }

        // the image is downloading in background
        if (null != mediaWorkerTaskForUrl(url)) {
            return null;
        }

        synchronized (sMemoryCache) {
            bitmap = sMemoryCache.get(url);
        }

        if (null == bitmap) {
            // if some medias are not found
            // do not try to reload them until the next application launch.
            synchronized (mFileNotFoundUrlsList) {
                if (mFileNotFoundUrlsList.indexOf(url) >= 0) {
                    bitmap = BitmapFactory.decodeResource(context.getResources(),
                            android.R.drawable.ic_menu_gallery);
                }
            }
        }

        // check if the image has not been saved in file system
        if ((null == bitmap) && (null != baseFile)) {
            String filename = null;

            // the url is a file one
            if (url.startsWith("file:")) {
                // try to parse it
                try {
                    Uri uri = Uri.parse(url);
                    filename = uri.getPath();

                } catch (Exception e) {
                }

                // cannot extract the filename -> sorry
                if (null == filename) {
                    return null;
                }
            }

            // not a valid file name
            if (null == filename) {
                filename = buildFileName(url, mimeType);
            }

            try {
                File file = filename.startsWith(File.separator) ? new File(filename)
                        : new File(baseFile, filename);

                if (!file.exists()) {
                    Log.d(LOG_TAG, "bitmapForURL() : " + filename + " does not exist");
                    return null;
                }

                FileInputStream fis = new FileInputStream(file);

                // read the metadata
                if (Integer.MAX_VALUE == rotation) {
                    rotation = ImageUtils.getRotationAngleForBitmap(context, Uri.fromFile(file));
                }

                if (null != fis) {
                    BitmapFactory.Options options = new BitmapFactory.Options();
                    options.inPreferredConfig = Bitmap.Config.ARGB_8888;

                    try {
                        bitmap = BitmapFactory.decodeStream(fis, null, options);
                    } catch (OutOfMemoryError error) {
                        System.gc();
                        Log.e(LOG_TAG, "bitmapForURL() : Out of memory 1 " + error);
                    }

                    //  try again
                    if (null == bitmap) {
                        try {
                            bitmap = BitmapFactory.decodeStream(fis, null, options);
                        } catch (OutOfMemoryError error) {
                            Log.e(LOG_TAG, "bitmapForURL() Out of memory 2" + error);
                        }
                    }

                    if (null != bitmap) {
                        synchronized (sMemoryCache) {
                            if (0 != rotation) {
                                try {
                                    android.graphics.Matrix bitmapMatrix = new android.graphics.Matrix();
                                    bitmapMatrix.postRotate(rotation);

                                    Bitmap transformedBitmap = Bitmap.createBitmap(bitmap, 0, 0,
                                            bitmap.getWidth(), bitmap.getHeight(), bitmapMatrix, false);
                                    bitmap.recycle();
                                    bitmap = transformedBitmap;
                                } catch (OutOfMemoryError ex) {
                                }
                            }

                            // cache only small images
                            // caching large images does not make sense
                            // it would replace small ones.
                            // let assume that the application must be faster when showing the chat history.
                            if ((bitmap.getWidth() < 1000) && (bitmap.getHeight() < 1000)) {
                                sMemoryCache.put(url, bitmap);
                            }
                        }
                    }

                    fis.close();
                }

            } catch (FileNotFoundException e) {
                Log.d(LOG_TAG, "bitmapForURL() : " + filename + " does not exist");
            } catch (Exception e) {
                Log.e(LOG_TAG, "bitmapForURL() " + e);

            }
        }
    }

    return bitmap;
}

From source file:com.apptentive.android.sdk.util.cache.ImageMemoryCache.java

public ImageMemoryCache(int maxMega) {
    bufferCache = new LruCache<String, Object>(maxMega * 1024 * 1024) { // by default use 1M as a unit for the in memory Lrucache
        /**/*from   w ww . ja va 2s.  c o  m*/
         * recycle the removed bitmap from memory
         */
        @Override
        protected void entryRemoved(boolean evicted, String key, Object oldValue, Object newValue) {
            if (oldValue != null) {
                if (oldValue instanceof Bitmap) {
                    ((Bitmap) oldValue).recycle();
                } else if (oldValue instanceof BitmapDrawable) {
                    ((BitmapDrawable) oldValue).getBitmap().recycle();
                }
                oldValue = null;
            }
        }

        @Override
        protected int sizeOf(String key, Object object) {
            // The cache size will be measured in bytes rather than
            // number of items.
            int byteCount = 0;

            if (object instanceof Bitmap) {
                byteCount = ((Bitmap) object).getRowBytes() * ((Bitmap) object).getHeight();
            } else if (object instanceof BitmapDrawable) {
                Bitmap bm = ((BitmapDrawable) object).getBitmap();
                byteCount = bm.getRowBytes() * bm.getHeight();
            }
            return byteCount;
        }
    };
}