List of usage examples for android.graphics Bitmap getByteCount
public final int getByteCount()
From source file:com.geniusgithub.contact.common.ContactPhotoManager.java
public ContactPhotoManagerImpl(Context context) { mContext = context;/*www. j ava2 s . c o m*/ final ActivityManager am = ((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE)); final float cacheSizeAdjustment = (am.isLowRamDevice()) ? 0.5f : 1.0f; final int bitmapCacheSize = (int) (cacheSizeAdjustment * BITMAP_CACHE_SIZE); mBitmapCache = new LruCache<Object, Bitmap>(bitmapCacheSize) { @Override protected int sizeOf(Object key, Bitmap value) { return value.getByteCount(); } @Override protected void entryRemoved(boolean evicted, Object key, Bitmap oldValue, Bitmap newValue) { if (DEBUG) dumpStats(); } }; final int holderCacheSize = (int) (cacheSizeAdjustment * HOLDER_CACHE_SIZE); mBitmapHolderCache = new LruCache<Object, BitmapHolder>(holderCacheSize) { @Override protected int sizeOf(Object key, BitmapHolder value) { return value.bytes != null ? value.bytes.length : 0; } @Override protected void entryRemoved(boolean evicted, Object key, BitmapHolder oldValue, BitmapHolder newValue) { if (DEBUG) dumpStats(); } }; mBitmapHolderCacheRedZoneBytes = (int) (holderCacheSize * 0.75); Log.i(TAG, "Cache adj: " + cacheSizeAdjustment); if (DEBUG) { Log.d(TAG, "Cache size: " + btk(mBitmapHolderCache.maxSize()) + " + " + btk(mBitmapCache.maxSize())); } mThumbnailSize = context.getResources().getDimensionPixelSize(R.dimen.contact_browser_list_item_photo_size); }
From source file:com.silentcircle.contacts.ContactPhotoManagerNew.java
@TargetApi(Build.VERSION_CODES.KITKAT) public ContactPhotoManagerImplNew(Context context) { mContext = context;/*from w w w . j a va 2s . com*/ final ActivityManager am = ((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE)); final float cacheSizeAdjustment = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) ? (am.isLowRamDevice() ? 0.5f : 1.0f) : 0.5f; final int bitmapCacheSize = (int) (cacheSizeAdjustment * BITMAP_CACHE_SIZE); mBitmapCache = new LruCache<Object, Bitmap>(bitmapCacheSize) { @Override protected int sizeOf(Object key, Bitmap value) { return value.getByteCount(); } @Override protected void entryRemoved(boolean evicted, Object key, Bitmap oldValue, Bitmap newValue) { if (DEBUG) dumpStats(); } }; final int holderCacheSize = (int) (cacheSizeAdjustment * HOLDER_CACHE_SIZE); mBitmapHolderCache = new LruCache<Object, BitmapHolder>(holderCacheSize) { @Override protected int sizeOf(Object key, BitmapHolder value) { return value.bytes != null ? value.bytes.length : 0; } @Override protected void entryRemoved(boolean evicted, Object key, BitmapHolder oldValue, BitmapHolder newValue) { if (DEBUG) dumpStats(); } }; mBitmapHolderCacheRedZoneBytes = (int) (holderCacheSize * 0.75); if (DEBUG) { Log.i(TAG, "Cache adj: " + cacheSizeAdjustment); Log.d(TAG, "Cache size: " + btk(mBitmapHolderCache.maxSize()) + " + " + btk(mBitmapCache.maxSize())); } mThumbnailSize = context.getResources().getDimensionPixelSize(R.dimen.contact_browser_list_item_photo_size); }
From source file:com.android.contacts.common.ContactPhotoManager.java
public ContactPhotoManagerImpl(Context context) { mContext = context;//from w w w. j av a2s .com final ActivityManager am = ((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE)); final float cacheSizeAdjustment = (am.isLowRamDevice()) ? 0.5f : 1.0f; final int bitmapCacheSize = (int) (cacheSizeAdjustment * BITMAP_CACHE_SIZE); mBitmapCache = new LruCache<Object, Bitmap>(bitmapCacheSize) { @Override protected int sizeOf(Object key, Bitmap value) { return value.getByteCount(); } @Override protected void entryRemoved(boolean evicted, Object key, Bitmap oldValue, Bitmap newValue) { if (DEBUG) dumpStats(); } }; final int holderCacheSize = (int) (cacheSizeAdjustment * HOLDER_CACHE_SIZE); mBitmapHolderCache = new LruCache<Object, BitmapHolder>(holderCacheSize) { @Override protected int sizeOf(Object key, BitmapHolder value) { return value.bytes != null ? value.bytes.length : 0; } @Override protected void entryRemoved(boolean evicted, Object key, BitmapHolder oldValue, BitmapHolder newValue) { if (DEBUG) dumpStats(); } }; mBitmapHolderCacheRedZoneBytes = (int) (holderCacheSize * 0.75); Log.i(TAG, "Cache adj: " + cacheSizeAdjustment); if (DEBUG) { Log.d(TAG, "Cache size: " + btk(mBitmapHolderCache.maxSize()) + " + " + btk(mBitmapCache.maxSize())); } mThumbnailSize = context.getResources().getDimensionPixelSize(R.dimen.contact_browser_list_item_photo_size); // Get a user agent string to use for URI photo requests. mUserAgent = UserAgentGenerator.getUserAgent(context); if (mUserAgent == null) { mUserAgent = ""; } }
From source file:com.android.contacts.common.ContactPhotoManager.java
/** * Dump cache stats on logcat.// www . ja v a 2s .c o m */ private void dumpStats() { if (!DEBUG) return; { int numHolders = 0; int rawBytes = 0; int bitmapBytes = 0; int numBitmaps = 0; for (BitmapHolder h : mBitmapHolderCache.snapshot().values()) { numHolders++; if (h.bytes != null) { rawBytes += h.bytes.length; } Bitmap b = h.bitmapRef != null ? h.bitmapRef.get() : null; if (b != null) { numBitmaps++; bitmapBytes += b.getByteCount(); } } Log.d(TAG, "L1: " + btk(rawBytes) + " + " + btk(bitmapBytes) + " = " + btk(rawBytes + bitmapBytes) + ", " + numHolders + " holders, " + numBitmaps + " bitmaps, avg: " + btk(safeDiv(rawBytes, numHolders)) + "," + btk(safeDiv(bitmapBytes, numBitmaps))); Log.d(TAG, "L1 Stats: " + mBitmapHolderCache.toString() + ", overwrite: fresh=" + mFreshCacheOverwrite.get() + " stale=" + mStaleCacheOverwrite.get()); } { int numBitmaps = 0; int bitmapBytes = 0; for (Bitmap b : mBitmapCache.snapshot().values()) { numBitmaps++; bitmapBytes += b.getByteCount(); } Log.d(TAG, "L2: " + btk(bitmapBytes) + ", " + numBitmaps + " bitmaps" + ", avg: " + btk(safeDiv(bitmapBytes, numBitmaps))); // We don't get from L2 cache, so L2 stats is meaningless. } }
From source file:com.cpic.taylor.logistics.activity.HomeActivity.java
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == CAMERA) { path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/userIcon.jpg"; if (!cameraUri.getPath().isEmpty()) { Bitmap temp = BitmapFactory.decodeFile(cameraUri.getPath()); Bitmap bitmap = big(temp, 60, 60); ivIcon.setImageBitmap(bitmap); changeInfo(USER_ICON);/*from ww w. j ava 2 s. c o m*/ } // upLoadUserIcon(new File(Environment.getExternalStorageDirectory() // .getAbsolutePath() + "/usericon.PNG")); } else if (requestCode == PHOTO) { if (data != null) { Uri uri = data.getData(); // uriContentProvider?sd? // decodeFile? // ContentResolver? ContentResolver cr = HomeActivity.this.getContentResolver(); try { Bitmap b = MediaStore.Images.Media.getBitmap(cr, uri); Bitmap bitmap = big(b, 60, 60); bitmap.getByteCount(); ivIcon.setImageBitmap(bitmap); // ? String[] proj = { MediaStore.Images.Media.DATA }; // ?android???Android Cursor cursor = HomeActivity.this.managedQuery(uri, proj, null, null, null); // ? int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); // ???? cursor.moveToFirst(); // ??? path = cursor.getString(column_index); changeInfo(USER_ICON); } catch (Exception e) { e.printStackTrace(); } } } else if (requestCode == INFO_CAMERA) { path1 = Environment.getExternalStorageDirectory().getAbsolutePath() + "/carInfo.jpg"; Log.i("oye", "" + path1); if (!cameraUri.getPath().isEmpty()) { Bitmap temp = BitmapFactory.decodeFile(cameraUri.getPath()); Bitmap bitmap = big(temp, 60, 60); ivCarInfo.setImageBitmap(bitmap); changeInfo(CAR_INFO); } } else if (requestCode == INFO_PHOTO) { if (data != null) { Uri uri = data.getData(); // uriContentProvider?sd? // decodeFile? // ContentResolver? ContentResolver cr = HomeActivity.this.getContentResolver(); try { Bitmap b = MediaStore.Images.Media.getBitmap(cr, uri); Bitmap bitmap = big(b, 60, 60); bitmap.getByteCount(); ivCarInfo.setImageBitmap(bitmap); // ? String[] proj = { MediaStore.Images.Media.DATA }; // ?android???Android Cursor cursor = HomeActivity.this.managedQuery(uri, proj, null, null, null); // ? int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); // ???? cursor.moveToFirst(); // ??? path1 = cursor.getString(column_index); changeInfo(CAR_INFO); // Log.i("oye", path); // ? // upLoadUserIcon(new File(path)); } catch (Exception e) { e.printStackTrace(); } } } }
From source file:com.android.contacts.common.ContactPhotoManager.java
/** * If necessary, decodes bytes stored in the holder to Bitmap. As long as the * bitmap is held either by {@link #mBitmapCache} or by a soft reference in * the holder, it will not be necessary to decode the bitmap. *//* ww w . ja v a 2 s .c o m*/ private static void inflateBitmap(BitmapHolder holder, int requestedExtent) { final int sampleSize = BitmapUtil.findOptimalSampleSize(holder.originalSmallerExtent, requestedExtent); byte[] bytes = holder.bytes; if (bytes == null || bytes.length == 0) { return; } if (sampleSize == holder.decodedSampleSize) { // Check the soft reference. If will be retained if the bitmap is also // in the LRU cache, so we don't need to check the LRU cache explicitly. if (holder.bitmapRef != null) { holder.bitmap = holder.bitmapRef.get(); if (holder.bitmap != null) { return; } } } try { Bitmap bitmap = BitmapUtil.decodeBitmapFromBytes(bytes, sampleSize); // TODO: As a temporary workaround while framework support is being added to // clip non-square bitmaps into a perfect circle, manually crop the bitmap into // into a square if it will be displayed as a thumbnail so that it can be cropped // into a circle. final int height = bitmap.getHeight(); final int width = bitmap.getWidth(); // The smaller dimension of a scaled bitmap can range from anywhere from 0 to just // below twice the length of a thumbnail image due to the way we calculate the optimal // sample size. if (height != width && Math.min(height, width) <= mThumbnailSize * 2) { final int dimension = Math.min(height, width); bitmap = ThumbnailUtils.extractThumbnail(bitmap, dimension, dimension); } // make bitmap mutable and draw size onto it if (DEBUG_SIZES) { Bitmap original = bitmap; bitmap = bitmap.copy(bitmap.getConfig(), true); original.recycle(); Canvas canvas = new Canvas(bitmap); Paint paint = new Paint(); paint.setTextSize(16); paint.setColor(Color.BLUE); paint.setStyle(Style.FILL); canvas.drawRect(0.0f, 0.0f, 50.0f, 20.0f, paint); paint.setColor(Color.WHITE); paint.setAntiAlias(true); canvas.drawText(bitmap.getWidth() + "/" + sampleSize, 0, 15, paint); } holder.decodedSampleSize = sampleSize; holder.bitmap = bitmap; holder.bitmapRef = new SoftReference<Bitmap>(bitmap); if (DEBUG) { Log.d(TAG, "inflateBitmap " + btk(bytes.length) + " -> " + bitmap.getWidth() + "x" + bitmap.getHeight() + ", " + btk(bitmap.getByteCount())); } } catch (OutOfMemoryError e) { // Do nothing - the photo will appear to be missing } }
From source file:com.silentcircle.contacts.ContactPhotoManager.java
/** * Checks if the photo is present in cache. If so, sets the photo on the view. * * @return false if the photo needs to be (re)loaded from the provider. *//* ww w.j a va2 s . c o m*/ private boolean loadCachedPhoto(ImageView view, Request request, boolean fadeIn) { BitmapHolder holder = mBitmapHolderCache.get(request.getKey()); if (holder == null) { // The bitmap has not been loaded ==> show default avatar request.applyDefaultImage(view); return false; } if (holder.bytes == null) { request.applyDefaultImage(view); return holder.fresh; } Bitmap cachedBitmap = holder.bitmapRef == null ? null : holder.bitmapRef.get(); if (cachedBitmap == null) { if (holder.bytes.length < 8 * 1024) { // Small thumbnails are usually quick to inflate. Let's do that on the UI thread inflateBitmap(holder, request.getRequestedExtent()); cachedBitmap = holder.bitmap; if (cachedBitmap == null) return false; } else { // This is bigger data. Let's send that back to the Loader so that we can // inflate this in the background request.applyDefaultImage(view); return false; } } final Drawable previousDrawable = view.getDrawable(); if (fadeIn && previousDrawable != null) { final Drawable[] layers = new Drawable[2]; // Prevent cascade of TransitionDrawables. if (previousDrawable instanceof TransitionDrawable) { final TransitionDrawable previousTransitionDrawable = (TransitionDrawable) previousDrawable; layers[0] = previousTransitionDrawable .getDrawable(previousTransitionDrawable.getNumberOfLayers() - 1); } else { layers[0] = previousDrawable; } layers[1] = new BitmapDrawable(mContext.getResources(), cachedBitmap); TransitionDrawable drawable = new TransitionDrawable(layers); view.setImageDrawable(drawable); drawable.startTransition(FADE_TRANSITION_DURATION); } else { view.setImageBitmap(cachedBitmap); } // Put the bitmap in the LRU cache. But only do this for images that are small enough // (we require that at least six of those can be cached at the same time) int byteCount = (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB_MR1) ? cachedBitmap.getRowBytes() * cachedBitmap.getHeight() : cachedBitmap.getByteCount(); if (byteCount < mBitmapCache.maxSize() / 6) { mBitmapCache.put(request.getKey(), cachedBitmap); } holder.bitmap = null; // Soften the reference return holder.fresh; }
From source file:com.silentcircle.contacts.ContactPhotoManagerNew.java
/** * Checks if the photo is present in cache. If so, sets the photo on the view. * * @return false if the photo needs to be (re)loaded from the provider. *///from ww w . j a v a 2 s .c o m private boolean loadCachedPhoto(ImageView view, Request request, boolean fadeIn) { BitmapHolder holder = mBitmapHolderCache.get(request.getKey()); if (holder == null) { // The bitmap has not been loaded ==> show default avatar request.applyDefaultImage(view, request.mIsCircular); return false; } if (holder.bytes == null) { request.applyDefaultImage(view, request.mIsCircular); return holder.fresh; } Bitmap cachedBitmap = holder.bitmapRef == null ? null : holder.bitmapRef.get(); if (cachedBitmap == null) { if (holder.bytes.length < 8 * 1024) { // Small thumbnails are usually quick to inflate. Let's do that on the UI thread inflateBitmap(holder, request.getRequestedExtent()); cachedBitmap = holder.bitmap; if (cachedBitmap == null) return false; } else { // This is bigger data. Let's send that back to the Loader so that we can // inflate this in the background request.applyDefaultImage(view, request.mIsCircular); return false; } } final Drawable previousDrawable = view.getDrawable(); if (fadeIn && previousDrawable != null) { final Drawable[] layers = new Drawable[2]; // Prevent cascade of TransitionDrawables. if (previousDrawable instanceof TransitionDrawable) { final TransitionDrawable previousTransitionDrawable = (TransitionDrawable) previousDrawable; layers[0] = previousTransitionDrawable .getDrawable(previousTransitionDrawable.getNumberOfLayers() - 1); } else { layers[0] = previousDrawable; } layers[1] = getDrawableForBitmap(mContext.getResources(), cachedBitmap, request); TransitionDrawable drawable = new TransitionDrawable(layers); view.setImageDrawable(drawable); drawable.startTransition(FADE_TRANSITION_DURATION); } else { view.setImageDrawable(getDrawableForBitmap(mContext.getResources(), cachedBitmap, request)); } // Put the bitmap in the LRU cache. But only do this for images that are small enough // (we require that at least six of those can be cached at the same time) if (cachedBitmap.getByteCount() < mBitmapCache.maxSize() / 6) { mBitmapCache.put(request.getKey(), cachedBitmap); } // hack: Don't allow resources from external requests to expire easily Uri uri = request.getUri(); if (uri != null) { final String scheme = uri.getScheme(); if (scheme.equals("http") || scheme.equals("https")) { holder.fresh = true; } } // Soften the reference holder.bitmap = null; return holder.fresh; }
From source file:com.android.contacts.common.ContactPhotoManager.java
/** * Checks if the photo is present in cache. If so, sets the photo on the view. * * @return false if the photo needs to be (re)loaded from the provider. */// w w w . j a v a 2s. c o m private boolean loadCachedPhoto(ImageView view, Request request, boolean fadeIn) { BitmapHolder holder = mBitmapHolderCache.get(request.getKey()); if (holder == null) { // The bitmap has not been loaded ==> show default avatar request.applyDefaultImage(view, request.mIsCircular); return false; } if (holder.bytes == null) { request.applyDefaultImage(view, request.mIsCircular); return holder.fresh; } Bitmap cachedBitmap = holder.bitmapRef == null ? null : holder.bitmapRef.get(); if (cachedBitmap == null) { if (holder.bytes.length < 8 * 1024) { // Small thumbnails are usually quick to inflate. Let's do that on the UI thread inflateBitmap(holder, request.getRequestedExtent()); cachedBitmap = holder.bitmap; if (cachedBitmap == null) return false; } else { // This is bigger data. Let's send that back to the Loader so that we can // inflate this in the background request.applyDefaultImage(view, request.mIsCircular); return false; } } final Drawable previousDrawable = view.getDrawable(); if (fadeIn && previousDrawable != null) { final Drawable[] layers = new Drawable[2]; // Prevent cascade of TransitionDrawables. if (previousDrawable instanceof TransitionDrawable) { final TransitionDrawable previousTransitionDrawable = (TransitionDrawable) previousDrawable; layers[0] = previousTransitionDrawable .getDrawable(previousTransitionDrawable.getNumberOfLayers() - 1); } else { layers[0] = previousDrawable; } layers[1] = getDrawableForBitmap(mContext.getResources(), cachedBitmap, request); TransitionDrawable drawable = new TransitionDrawable(layers); view.setImageDrawable(drawable); drawable.startTransition(FADE_TRANSITION_DURATION); } else { view.setImageDrawable(getDrawableForBitmap(mContext.getResources(), cachedBitmap, request)); } // Put the bitmap in the LRU cache. But only do this for images that are small enough // (we require that at least six of those can be cached at the same time) if (cachedBitmap.getByteCount() < mBitmapCache.maxSize() / 6) { mBitmapCache.put(request.getKey(), cachedBitmap); } // Soften the reference holder.bitmap = null; return holder.fresh; }
From source file:com.android.contacts.ContactPhotoManager.java
/** * Checks if the photo is present in cache. If so, sets the photo on the view. * * @return false if the photo needs to be (re)loaded from the provider. *//* ww w . jav a 2 s. c o m*/ private boolean loadCachedPhoto(ImageView view, Request request, boolean fadeIn) { BitmapHolder holder = mBitmapHolderCache.get(request.getKey()); if (holder == null) { // The bitmap has not been loaded ==> show default avatar request.applyDefaultImage(view, request.mIsCircular); return false; } if (holder.bytes == null || holder.bytes.length == 0) { request.applyDefaultImage(view, request.mIsCircular); return holder.fresh; } Bitmap cachedBitmap = holder.bitmapRef == null ? null : holder.bitmapRef.get(); if (cachedBitmap == null) { if (holder.bytes.length < 8 * 1024) { // Small thumbnails are usually quick to inflate. Let's do that on the UI thread inflateBitmap(holder, request.getRequestedExtent()); cachedBitmap = holder.bitmap; if (cachedBitmap == null) return false; } else { // This is bigger data. Let's send that back to the Loader so that we can // inflate this in the background request.applyDefaultImage(view, request.mIsCircular); return false; } } final Drawable previousDrawable = view.getDrawable(); if (fadeIn && previousDrawable != null) { final Drawable[] layers = new Drawable[2]; // Prevent cascade of TransitionDrawables. if (previousDrawable instanceof TransitionDrawable) { final TransitionDrawable previousTransitionDrawable = (TransitionDrawable) previousDrawable; layers[0] = previousTransitionDrawable .getDrawable(previousTransitionDrawable.getNumberOfLayers() - 1); } else { layers[0] = previousDrawable; } layers[1] = getDrawableForBitmap(mContext.getResources(), cachedBitmap, request); TransitionDrawable drawable = new TransitionDrawable(layers); view.setImageDrawable(drawable); drawable.startTransition(FADE_TRANSITION_DURATION); } else { view.setImageDrawable(getDrawableForBitmap(mContext.getResources(), cachedBitmap, request)); } // Put the bitmap in the LRU cache. But only do this for images that are small enough // (we require that at least six of those can be cached at the same time) if (cachedBitmap.getByteCount() < mBitmapCache.maxSize() / 6) { mBitmapCache.put(request.getKey(), cachedBitmap); } // Soften the reference holder.bitmap = null; return holder.fresh; }