android


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.

Related Links

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

Categories

HOME
turing-machines
gdb
apache-flink
ecmascript-6
pagination
google-spreadsheet
powerpoint
interface
battery
pclxl
seo
static-site
yandex
tivoli
emscripten
polling
special-characters
intercom
android-intent
docx
http-post
capistrano3
red5
forgot-password
charles
blade
postscript
jboss6.x
onload
tpm
laravel-eloquent
latitude-longitude
magento-1.9.2.4
openalpr
graphhopper
nohup
metaprogramming
stringtokenizer
dbnull
movilizer
java-bytecode-asm
headless
check-mk
polyline
vcloud-director-rest-api
simplepie
angular2-testing
custom-lists
preprocessor
myspace
tinkerpop3
spring-cloud-feign
uicolor
mongodb-php
gogs
eigenvector
freepbx
cda
phpgrid
ascii-art
asyncdisplaykit
highland.js
relativelayout
dynamics-nav-2016
themoviedb-api
django-urls
video-player
wso2greg
snoop
osc
phpldapadmin
document-repository
boost-program-options
uncaught-typeerror
ghcjs
punycode
webfinger
nsmenu
cuba
reflector
malformedurlexception
dct
draw2d-js
clickbank
amslidemenu
observium
nevron
poker
monotouch.dialog
correlated-subquery
apache-commons-dbutils
node-redis
xcelsius
umra
dot-emacs
folding
zen
qtabbar
change-management
leader
idatareader

Resources

Encrypt Message