Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a Bitmap is empty (blank) on Android

Tags:

How can I check if a Bitmap object is completely blank, i.e. all its pixels are transparent, without a x-y loop on every pixel?

like image 468
lorenzo-s Avatar asked Feb 27 '15 14:02

lorenzo-s


People also ask

How do I know if a bitmap is empty?

You can do a check when you want to return the BitMap look to see if the ArrayList of Paths is bigger than 0 and return the BitMap if so, or else return null.

Can a bitmap be null?

asShared. Return an immutable bitmap backed by shared memory which can be efficiently passed between processes via Parcelable. If this bitmap already meets these criteria it will return itself. This value cannot be null .


2 Answers

You can check your Bitmap instance (in the example myBitmap) against an empty one with:

Bitmap emptyBitmap = Bitmap.createBitmap(myBitmap.getWidth(), myBitmap.getHeight(), myBitmap.getConfig()); if (myBitmap.sameAs(emptyBitmap)) {     // myBitmap is empty/blank } 
like image 104
lorenzo-s Avatar answered Sep 18 '22 10:09

lorenzo-s


This is not an efficient way to solve the problem but it does its purpose cause I didn't find a solution even in docs

see while the Bitmap is null it's width or height can't be fetched and it raises a crash so here is may solve your problem

       try{                    int check=bitmap.getWidth();                // the bitmap is valid & not null or empty                  }catch (Exception w){                   // the bitmap, not valid eighter null or empty                 } 

hope this helps :)

like image 38
Hossam Ali Avatar answered Sep 20 '22 10:09

Hossam Ali