Is this a correct way to get a bitmap's size before decoding it into memory?
I know I can use these ways to get the Bitmap's size: bitmap.getAllocationByteCount(); //API 19 bitmap.getByteCount(); //API 12 bitmap.getRowBytes() * bitmap.getHeight(); //earlier version But This all need a Bitmap object which mean I need to decode the bitmap into memory before, this may case OOM Exception. So I use this way to get the size before I can get Bitmap object: BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(bitmapPath, options); int picWidth = options.outWidth; int picHeight = options.outHeight; int size = 4 * picHeight * picWidth; //Byte I think it cost 32 bits per pixel because Android decode bitmap use RGB_8888 default. Is this a correct way or is there a better way to do this ?
You can select the bitmap configuration with BitmapFactory.Options.inPreferredConfig. This will allow you to specify a configuration where you know for sure how many bytes per pixel will be occupied by the Bitmap. I believe RGB_8888 is the default. You can probably not 100% reliably prevent an OOM on a bitmap decode since you don't have a guarantee for a set amount of contiguous free space in memory for the allocation of the Bitmap. But you can certainly adjust your sample size and config to reduce the load.
Just create a File object with the path to the Bitmap. Then get file.length() to get the file size. e.g. File bitmap = new File(pathToBitmap); bitmap.length(); You'll have to put a try/catch clause as required.
Android How to tell if Internet connection is being used?
ContentObserver for SQLite?
Google Maps, show close places to current user poisition
Indic Language Support for SDK but not PhoneGap
How to convert json string value into an ArrayList and display the values in Android ListView [duplicate]
Programmatically change RelativeLayout toRightOf to toLeftOf
cant see my list items
Set rowSpan or colSpan of a child of a GridLayout programmatically?
Android Intent from Class With On Receive
MasterDetailFlow error: “Multiple markers at this line”
the android emulator does not start, avd
Focus prevents onclick
How to make video player like native in android
Enabling USB debugging after re flashing the phone
Dialog window behavior in screen rotation: Why the settext doesn't update?
loading a Unity Vuforia Project in a Subview in Android throws Fatal Exception