Example usage for android.graphics Bitmap getByteCount

List of usage examples for android.graphics Bitmap getByteCount

Introduction

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

Prototype

public final int getByteCount() 

Source Link

Document

Returns the minimum number of bytes that can be used to store this bitmap's pixels.

Usage

From source file:com.stargame.ad.util.cache.image.AbImageBaseCache.java

/**
 * .//from  w w w. j a v a 2s . com
 */
public AbImageBaseCache() {
    super();
    int maxSize = AppConfig.MAX_CACHE_SIZE_INBYTES;
    mCache = new LruCache<String, Bitmap>(maxSize) {
        @Override
        protected int sizeOf(String key, Bitmap value) {
            try {
                return value.getRowBytes() * value.getHeight();
            } catch (Exception e) {
                // e.printStackTrace();
                return value.getByteCount();
            }
        }
    };
}

From source file:me.vijayjaybhay.galleryview.cache.MemoryImageCache.java

/**
 * Computes number of bytes in bitmap/*from  w  ww. j  a  va 2 s  .  com*/
 * @param data Bitmap data
 * @return  number of bytes in bitmap
 */
@TargetApi(Build.VERSION_CODES.KITKAT)
protected int byteSizeOf(Bitmap data) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB_MR1) {
        return data.getRowBytes() * data.getHeight();
    } else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
        return data.getByteCount();
    } else {
        return data.getAllocationByteCount();
    }
}

From source file:net.gree.asdk.core.imageloader.cache.ImageCache.java

@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
private int getBitmapSize(Bitmap bitmap) {
    if (Util.hasHoneycombMR1()) {
        return bitmap.getByteCount();
    }//  w  ww  .  j av a  2 s. c  om
    return bitmap.getRowBytes() * bitmap.getHeight();
}

From source file:com.meg7.soas.util.BitmapLruCache.java

@SuppressLint("NewApi")
@Override/*ww  w  .j  a v  a  2s.  co m*/
protected int sizeOf(String key, Bitmap bitmap) {
    // The cache size will be measured in kilobytes rather than
    // number of items.
    if (Build.VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
        return bitmap.getAllocationByteCount() / 1024;
    } else if (Build.VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB_MR1) {
        return bitmap.getByteCount() / 1024;
    } else {
        return bitmap.getRowBytes() * bitmap.getHeight() / 1024;
    }
}

From source file:com.lovebridge.chat.view.ImageCache.java

/**
 * @param candidate - Bitmap to check//from  www .  j a va2 s .  c  o  m
 * @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>
 */
@TargetApi(19)
private static boolean canUseForInBitmap(Bitmap candidate, BitmapFactory.Options targetOptions) {
    // BEGIN_INCLUDE(can_use_for_inbitmap)
    if (!com.lovebridge.chat.utils.Utils.hasKitKat()) {
        // 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.getByteCount();
    // END_INCLUDE(can_use_for_inbitmap)
}

From source file:com.friedran.appengine.dashboard.gui.DashboardLoadFragment.java

private LruCache<String, Bitmap> initChartsMemoryCache() {
    final int maxMemoryInKBs = (int) (Runtime.getRuntime().maxMemory() / 1024);
    final int cacheSize = maxMemoryInKBs / 5;

    return new LruCache<String, Bitmap>(cacheSize) {
        @Override/*from  w ww . ja v a2s  .  c  o  m*/
        protected int sizeOf(String key, Bitmap bitmap) {
            // Measured in kilobytes
            return bitmap.getByteCount() / 1024;
        }
    };
}

From source file:com.liangxun.university.huanxin.chat.video.util.VideoImageCache.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>
 *//*from ww  w. j a  va  2s.  co m*/
@TargetApi(19)
private static boolean canUseForInBitmap(Bitmap candidate, BitmapFactory.Options targetOptions) {
    // BEGIN_INCLUDE(can_use_for_inbitmap)
    if (!HxUtils.hasKitKat()) {
        // 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.getByteCount();
    // END_INCLUDE(can_use_for_inbitmap)
}

From source file:com.xpple.jahoqy.util.videoImageCache.java

/**
 * @param candidate// ww w.  j a v  a2 s .c  o m
 *            - 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>
 */
private static boolean canUseForInBitmap(Bitmap candidate, BitmapFactory.Options targetOptions) {
    // BEGIN_INCLUDE(can_use_for_inbitmap)
    if (!Utils.hasKitKat()) {
        // 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.getByteCount();
    // END_INCLUDE(can_use_for_inbitmap)
}

From source file:cmu.cconfs.instantMessage.util.ImageCache.java

/**
 * @param candidate/*from  www  .  ja va  2 s  .  c o m*/
 *            - 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>
 */
@TargetApi(19)
private static boolean canUseForInBitmap(Bitmap candidate, BitmapFactory.Options targetOptions) {
    // BEGIN_INCLUDE(can_use_for_inbitmap)
    if (!Utils.hasKitKat()) {
        // 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.getByteCount();
    // END_INCLUDE(can_use_for_inbitmap)
}

From source file:com.nike.plusgps.nikeplusgallery.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    dbHelper = new DBHelper(this); // Creates the database
    dbHelper.deleteAllResponse(); // Clears the JSON response cache
    dbHelper.deleteAllMedia(); // Clears the JSON images cache

    // Get memory class of this device, exceeding this amount will throw an
    // OutOfMemory exception.
    final int memClass = ((ActivityManager) getSystemService(Context.ACTIVITY_SERVICE)).getMemoryClass();
    // Use 1/8th of the available memory for this memory cache.
    final int cacheSize = 1024 * 1024 * memClass / 8;
    mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
        @Override/*  w ww  .  j  a v a2s  . co m*/
        protected int sizeOf(String key, Bitmap bitmap) {
            // The cache size will be measured in bytes rather than number of items.
            return bitmap.getByteCount();
        }
    };

    if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {

        setContentView(R.layout.activity_main_port);
        GridView view = (GridView) findViewById(R.id.gridView);
        flickrFeedList = new ArrayList<FlickrFeed>();
        new JSONParser()
                .execute("http://api.flickr.com/services/feeds/photos_public.gne?tags=nike&format=json");
        adapter = new FlickrFeedAdapter(getApplicationContext(), R.layout.single_elem_port, flickrFeedList,
                dbHelper, mMemoryCache);
        view.setAdapter(adapter);

    } else {

        setContentView(R.layout.activity_main_land);
        carouselElement = (LinearLayout) findViewById(R.id.carousel);
        // Compute width of a carousel item based on screen width and initial item count
        final DisplayMetrics displayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        final int imageWidth = (int) (displayMetrics.widthPixels / INITIAL_ITEMS_COUNT);

        ImageView imageItem;
        String imgURL;
        for (int i = 0; i < flickrFeedList.size(); ++i) {
            imageItem = new ImageView(this);
            imgURL = flickrFeedList.get(i).getMedia();
            new DownloadImageTask(imageItem).execute(imgURL);
            imageItem.setLayoutParams(new LinearLayout.LayoutParams(imageWidth, imageWidth));
            carouselElement.addView(imageItem);
        }
    }
}