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:ustc.bitmap.imagecache.ImageCache.java

/**
 * @param candidate - Bitmap to check//from   w  ww . j  a  v  a2 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>
 */
private static boolean canUseForInBitmap(Bitmap candidate, BitmapFactory.Options targetOptions) {

    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.getAllocationByteCount();
    return byteCount <= candidate.getByteCount();
}

From source file:com.soft.pushsender.DemoActivity.java

private void initCache() {

    if (mMemoryCache == null) {

        final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);

        final int cacheSize = maxMemory / 8;

        mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
            @Override//from  ww w. ja va  2s. co m
            protected int sizeOf(String key, Bitmap bitmap) {
                // The cache size will be measured in kilobytes rather than
                // number of items.
                return bitmap.getByteCount() / 1024;
            }
        };
    }
}

From source file:air.com.snagfilms.utils.ImageCache.java

/**
 * @param candidate//from   w  ww  . j ava 2s  .com
 *            - 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(VERSION_CODES.JELLY_BEAN_MR1)
private static boolean canUseForInBitmap(Bitmap candidate, BitmapFactory.Options targetOptions) {

    /*
     * 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();
}

From source file:com.witheyjr.listviewanimator.StableWrapperArrayAdapter.java

public StableWrapperArrayAdapter(Context context, int layoutResourceId, List<ContentsWrapper> origObjects) {
    super(context, layoutResourceId, origObjects);
    this.mObjects = origObjects;
    for (int i = 0; i < mObjects.size(); ++i) {
        mIdMap.put(mObjects.get(i), i);//from   w  w  w  .j a v  a  2s .  c  o m
    }
    this.mContext = context;
    this.mLayoutResourceId = layoutResourceId;
    withImage = true;
    /* 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/4 of the available memory for this memory cache.
    final int cacheSize = maxMemory / 4;
    mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
        @SuppressLint("NewApi")
        @Override
        protected int sizeOf(String key, Bitmap bitmap) {
            // The cache size will be measured in kilobytes rather than number of items. Also, I love fragmentation.
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB_MR1) {
                return (bitmap.getRowBytes() * bitmap.getHeight()) / 1024;
            } else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
                return bitmap.getByteCount() / 1024;
            } else {
                return bitmap.getAllocationByteCount() / 1024;
            }
        }
    };
}

From source file:com.laer.easycast.ImagePane.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mPlaceHolderBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.loading);
    // 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;

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

}

From source file:com.example.xyzreader.cp7.ArticleListFragment.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // 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;

    mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
        @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
        @Override/* www . ja v a  2s  .c om*/
        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 > Build.VERSION_CODES.HONEYCOMB_MR1) {
                return bitmap.getByteCount() / 1024;
            } else {
                return bitmap.getRowBytes() * bitmap.getHeight() / 1024;
            }
        }
    };

    setListAdapter(new MyAdapter());
    setHasOptionsMenu(true);
}

From source file:com.example.xyzreader.cp8.ArticleListFragment.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // 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;

    mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
        @TargetApi(12)//from  ww w. j  a  v a 2s.  c  om
        @Override
        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 > Build.VERSION_CODES.HONEYCOMB_MR1) {
                return bitmap.getByteCount() / 1024;
            } else {
                return bitmap.getRowBytes() * bitmap.getHeight() / 1024;
            }
        }
    };

    setListAdapter(new MyAdapter());
    setHasOptionsMenu(true);
}

From source file:org.vqeg.viqet.activities.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //set orientation potrait if its phone
    if (getResources().getBoolean(R.bool.portrait_only)) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    } else {/*from  w ww .  jav a  2  s  . co m*/
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    }

    // 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;

    mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
        @Override
        protected int sizeOf(String key, Bitmap bitmap) {
            // The cache size will be measured in kilobytes rather than
            // number of items.
            return bitmap.getByteCount() / 1024;
        }
    };

    this.mainPage = findViewById(R.id.drawer_layout);
    this.downloadScreen = findViewById(R.id.downloading_screen);
    this.internetRequiredScreen = findViewById(R.id.internet_required_screen);

    mTitle = mDrawerTitle = "VIQET";
    mOptionsTitles = getResources().getStringArray(R.array.options_array);
    mImageResources = new int[] { R.drawable.home, R.drawable.camera_results_icon,
            R.drawable.photo_quality_results, R.drawable.help, R.drawable.about, R.drawable.setting };
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.left_drawer);

    // set a custom shadow that overlays the main content when the drawer opens
    mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

    // set up the drawer's list view with items and click listener
    mDrawerList.setAdapter(new CustomDrawerListAdapter(this, mOptionsTitles, mImageResources));
    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

    // enable ActionBar app icon to behave as action to toggle nav drawer
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);

    // ActionBarDrawerToggle ties together the the proper interactions
    // between the sliding drawer and the action bar app icon
    mDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
            mDrawerLayout, /* DrawerLayout object */
            R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
            R.string.drawer_open, /* "open drawer" description for accessibility */
            R.string.drawer_close /* "close drawer" description for accessibility */
    ) {
        public void onDrawerClosed(View view) {
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }

        public void onDrawerOpened(View drawerView) {
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }
    };
    mDrawerLayout.setDrawerListener(mDrawerToggle);

    if (savedInstanceState == null) {
        //todo check
        selectItem(0);
        lastClicked = 0;
    } else {
        int pos = savedInstanceState.getInt("FRAGMENT");
        selectItem(pos);
        lastClicked = pos;
    }

    Button retryDownloadButton = (Button) findViewById(R.id.retryDownload);
    retryDownloadButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (SystemInfo.isNetworkPresent(getApplicationContext())) {
                downloadScreen.setVisibility(View.VISIBLE);
                internetRequiredScreen.setVisibility(View.INVISIBLE);
                downloadRemoteInfo(PhotoInspectorService.FETCH_VERSION_ACTION);
            } else {
                Toast.makeText(getApplicationContext(), getString(R.string.noInternetError), Toast.LENGTH_LONG)
                        .show();
            }
        }
    });

    //Check remote info
    RemoteInfo remoteInfo = RemoteInfoProvider.getRemoteInfo();

    if (remoteInfo == null) {
        mainPage.setVisibility(View.INVISIBLE);
        if (SystemInfo.isNetworkPresent(getApplicationContext())) {
            downloadScreen.setVisibility(View.VISIBLE);
            internetRequiredScreen.setVisibility(View.INVISIBLE);
            downloadRemoteInfo(PhotoInspectorService.FETCH_VERSION_ACTION);
        } else {
            downloadScreen.setVisibility(View.INVISIBLE);
            internetRequiredScreen.setVisibility(View.VISIBLE);
        }
    } else {
        //downloadRemoteInfo(PhotoInspectorService.FETCH_VERSION_MODEL_CHANGED_ACTION);
        downloadRemoteInfo(PhotoInspectorService.INSPECT_ALL_PENDING_PHOTOS_ACTION);
    }
    checkFirstRun();
}

From source file:com.floyd.diamond.IMImageCache.java

/**
 * Initialize the cache, providing all parameters.
 *
 * @param context     The context to use
 * @param cacheParams The cache parameters to initialize the cache
 *///from  w w w  .jav  a 2  s.co m
private void init(Context context, ImageCacheParams cacheParams) {
    // final File diskCacheDir = DiskLruCache.getDiskCacheDir(context,
    // cacheParams.uniqueName);

    // Set up disk cache
    //      if (cacheParams.diskCacheEnabled) {
    //         mDiskCache = new WxImageDiskCache(cacheParams.uniqueName);
    //      }

    // 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
             */
            @SuppressLint("NewApi")
            @Override
            protected int sizeOf(String key, Bitmap bitmap) {
                // int size = bitmap.getRowBytes() * bitmap.getHeight();
                // Log.d("setting", "Bitmap size:" + size);
                int size = 0;
                if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB_MR1) {
                    size = bitmap.getRowBytes() * bitmap.getHeight();
                } else {
                    size = bitmap.getByteCount();
                }
                return size;
            }
        };
    }

}

From source file:io.mapsquare.osmcontributor.ui.utils.BitmapHandler.java

@Inject
public BitmapHandler(Application osmTemplateApplication) {
    context = osmTemplateApplication.getApplicationContext();

    final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
    // Use 1/5th of the available memory for this memory cache.
    final int cacheSize = maxMemory / 5;

    cache = new LruCache<String, Bitmap>(cacheSize) {
        @Override/*from  w ww  .j  ava 2 s.  c  om*/
        protected int sizeOf(String key, Bitmap bitmap) {
            // The cache size will be measured in kilobytes rather than items count
            return bitmap.getByteCount() / 1024;
        }
    };

    icons.put("administrative", R.drawable.administrative);
    icons.put("animal_shelter", R.drawable.pet2);
    icons.put("antique", R.drawable.archaeological);
    icons.put("gallery", R.drawable.art_gallery2);
    icons.put("boat_sharing", R.drawable.sailing);
    icons.put("books", R.drawable.library);
    icons.put("boutique", R.drawable.convenience);
    icons.put("brothel", R.drawable.hotel2);
    icons.put("bureau_de_change", R.drawable.currency_exchange);
    icons.put("camp_site", R.drawable.camping);
    icons.put("car_wash", R.drawable.ford);
    icons.put("car_sharing", R.drawable.car_share);
    icons.put("clinic", R.drawable.doctors2);
    icons.put("arts_centre", R.drawable.art_gallery2);
    icons.put("dry_cleaning", R.drawable.laundrette);
    icons.put("archaeological_site", R.drawable.archaeological2);
    icons.put("farm", R.drawable.marketplace);
    icons.put("variety_store", R.drawable.convenience);
    icons.put("food_court", R.drawable.marketplace);
    icons.put("fort", R.drawable.castle2);
    icons.put("general", R.drawable.department_store);
    icons.put("hardware", R.drawable.mine);
    icons.put("doityourself", R.drawable.diy);
    icons.put("organic", R.drawable.greengrocer);
    icons.put("laundry", R.drawable.laundrette);
    icons.put("kindergarten", R.drawable.nursery3);
    icons.put("manor", R.drawable.house);
    icons.put("alpine_hut", R.drawable.alpinehut);
    icons.put("wilderness_hut", R.drawable.alpinehut);
    icons.put("ship", R.drawable.sailing);
    icons.put("music_venue", R.drawable.music);
    icons.put("music_venue", R.drawable.music);
    icons.put("nursing_home", R.drawable.hotel2);
    icons.put("optician", R.drawable.opticians);
    icons.put("viewpoint", R.drawable.photo);
    icons.put("crossing", R.drawable.zebra_crossing);
    icons.put("guest_house", R.drawable.bed_and_breakfast2);
    icons.put("photo_booth", R.drawable.photo);
    icons.put("picnic_site", R.drawable.picnic);
    icons.put("services", R.drawable.picnic);
    icons.put("public_bookcase", R.drawable.library);
    icons.put("register_office", R.drawable.administrative);
    icons.put("mini_roundabout", R.drawable.roundabout_anticlockwise);
    icons.put("ruins", R.drawable.ruin);
    icons.put("caravan_site", R.drawable.caravan_park);
    icons.put("tailor", R.drawable.clothes);
    icons.put("taxi", R.drawable.taxi_rank);
    icons.put("tomb", R.drawable.memorial);
    icons.put("travel_agency", R.drawable.aerodrome);
    icons.put("video", R.drawable.video_rental);
    icons.put("waste_basket", R.drawable.waste_bin);
    icons.put("waste_basket", R.drawable.waste_bin);
    icons.put("artwork", R.drawable.art_gallery);
    icons.put("aerodrome", R.drawable.aerodrome);
    icons.put("aircraft", R.drawable.aerodrome);
    icons.put("alcohol", R.drawable.bar);
    icons.put("atm", R.drawable.atm);
    icons.put("attraction", R.drawable.attraction);
    icons.put("bank", R.drawable.bank);
    icons.put("bar", R.drawable.bar);
    icons.put("bus_stop", R.drawable.bus_stop);
    icons.put("bicycle_parking", R.drawable.bicycle_parking);
    icons.put("bicycle_rental", R.drawable.bicycle_rental);
    icons.put("biergarten", R.drawable.biergarten);
    icons.put("cafe", R.drawable.cafe);
    icons.put("car_rental", R.drawable.car_rental);
    icons.put("church", R.drawable.place_of_worship);
    icons.put("cinema", R.drawable.cinema);
    icons.put("city", R.drawable.town);
    icons.put("commercial", R.drawable.mall);
    icons.put("courthouse", R.drawable.courthouse);
    icons.put("dentist", R.drawable.dentist);
    icons.put("doctors", R.drawable.doctors);
    icons.put("drinking_water", R.drawable.drinking_water);
    icons.put("embassy", R.drawable.embassy);
    icons.put("entrance", R.drawable.entrance);
    icons.put("fast_food", R.drawable.fast_food);
    icons.put("fire_station", R.drawable.fire_station);
    icons.put("fuel", R.drawable.fuel);
    icons.put("hamlet", R.drawable.town);
    icons.put("hospital", R.drawable.hospital);
    icons.put("hotel", R.drawable.hotel);
    icons.put("house", R.drawable.house);
    icons.put("housenumber", R.drawable.house);
    icons.put("hunting_stand", R.drawable.hunting_stand);
    icons.put("locality", R.drawable.town);
    icons.put("mall", R.drawable.mall);
    icons.put("nightclub", R.drawable.nightclub);
    icons.put("neighbourhood", R.drawable.house);
    icons.put("parking", R.drawable.parking);
    icons.put("pharmacy", R.drawable.pharmacy);
    icons.put("place_of_worship", R.drawable.place_of_worship);
    icons.put("police", R.drawable.police);
    icons.put("political", R.drawable.administrative);
    icons.put("primary", R.drawable.street);
    icons.put("prison", R.drawable.prison);
    icons.put("pub", R.drawable.pub);
    icons.put("recycling", R.drawable.recycling);
    icons.put("religious_administrative", R.drawable.place_of_worship);
    icons.put("residential", R.drawable.house);
    icons.put("restaurant", R.drawable.restaurant);
    icons.put("retail", R.drawable.mall);
    icons.put("road", R.drawable.street);
    icons.put("secondary", R.drawable.street);
    icons.put("stadium", R.drawable.stadium);
    icons.put("station", R.drawable.bus_stop);
    icons.put("street", R.drawable.street);
    icons.put("suburb", R.drawable.town);
    icons.put("subway_entrance", R.drawable.bus_stop);
    icons.put("supermarket", R.drawable.mall);
    icons.put("terminal", R.drawable.aerodrome);
    icons.put("tertiary", R.drawable.street);
    icons.put("theatre", R.drawable.theatre);
    icons.put("toilets", R.drawable.toilets);
    icons.put("town", R.drawable.town);
    icons.put("townhall", R.drawable.townhall);
    icons.put("track", R.drawable.street);
    icons.put("village", R.drawable.town);
    icons.put("phone", R.drawable.sos);
}