Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //To load a bitmap with out causes memory issues, it must be scaled before loaded into memory
- public Bitmap createBitmap() {
- BitmapFactory.Options options2 = new BitmapFactory.Options();
- //inJustDecodeBounds basically loads the spec of the bitmap, such as height and width
- options2.inJustDecodeBounds = true;
- BitmapFactory.decodeFile(Main.this.getExternalFilesDir(filepath) +"/companylogo.png",options2);
- //calculateInSampleSize finds the appropriate size to scale to
- options2.inSampleSize = calculateInSampleSize(options2);
- options2.inJustDecodeBounds = false;
- return BitmapFactory.decodeFile(Main.this.getExternalFilesDir(filepath) +"/companylogo.png",options2);
- }
- public int calculateInSampleSize(BitmapFactory.Options options) {
- //get the width of device in pixels
- DisplayMetrics displayMetrics = Main.this.getResources().getDisplayMetrics();
- int reqWidth = displayMetrics.widthPixels;
- //get the bitmapInHeight/width of incoming bitmap
- final int bitmapInHeight = options.outHeight;
- final int bitmapInWidth = options.outWidth;
- //this calculates the maximum height/width that can fit on the screen, keeping the ratio size
- double devCal2 = (bitmapInHeight*1000)/bitmapInWidth;
- int reqHeight = (int) ((devCal2/1000)*reqWidth);
- int inSampleSize = 1;
- if (bitmapInHeight > reqHeight || bitmapInWidth > reqWidth) {
- final int halfHeight = bitmapInHeight / 2;
- final int halfWidth = bitmapInWidth / 2;
- // Calculate the largest inSampleSize value that is a power of 2 and keeps both
- // bitmapInHeight and width larger than the requested bitmapInHeight and width.
- while ((halfHeight / inSampleSize) > reqHeight
- && (halfWidth / inSampleSize) > reqWidth) {
- inSampleSize *= 2;
- }
- }
- return inSampleSize;
- // }
- //});
- //}
- //}.start();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement