BitmapFactory.decodeFile() returning null

Started by
2 comments, last by grumpyOldDude 8 years, 11 months ago

Hello,

right now i 'm being driven to the brink of madness by this particular problem.

I've used BitmapFactory.decodeFile() in the past many times, but for some reasons unknown to me now its returning null. I have quadruple checked (X 100 ) the file path, image size (800x400, 80kb), image type, the code... but so far i cant see no reasons it should be returning null object

Platform - android, java, eclipse

Code is below. BTW "save_bm_image" function has all the code to save files and works, I'm saving the file just after loading as this a debugging-diagnostic step

I need help to know why BitmapFactory.decodeFile is returning null?

Many thanks in advance


   CAMERA_PIC_DIR = "/DCIM/Camera/blocks";
   ImageDir = Environment.getExternalStorageDirectory().getAbsolutePath() + CAMERA_PIC_DIR;
  .  .  .

  Bitmap WBL_reloaded   = BitmapFactory.decodeFile(ImageDir+"WBL_a10.jpg");

   if( WBL_reloaded != null )   save_bm_image( WBL_reloaded, ImageDir,  "IMG1.png" );
   else                         Log.v(tag, "nulL!!. load failed");


  BitmapFactory.Options options = new BitmapFactory.Options();
  options.inSampleSize = 4;

  Bitmap BL_reloaded   = BitmapFactory.decodeFile( ImageDir+"BL_a10.jpg",  options );
 
  if( BL_reloaded != null )   save_bm_image( BL_reloaded,  ImageDir, "IMG2.png" );
  else                        Log.v(tag, "nulL!!. load failed");

can't help being grumpy...

Just need to let some steam out, so my head doesn't explode...

Advertisement

Can't say I have ever enjoyed BitmapFactory on Android.

I can't recall where but I have heard there can be issues with calling decodeFile. I changed mine to load the image into a buffer and then use BitmapFactory.decodeByteArray. This will allow you to log stuff like whether it was even found on the device and if it's the correct size. You could also do a MD5 and see if the image looks corrupt. Sounds like overkill but it might show something.

IIRC the Android build process will optimize your png files unless you tell it not to. Should be ok but maybe toggle it and see what happens.

It could be that the file itself is corrupt or not built exactly right. Some images made using a custom tool could be generating an image that most programs can load as they are less picky or can handle errors better. Try and load the file in something like Photoshop and resave it.

Finally I wouldn't format my if/else like that either. Playing with fire. When your code goes live, the Play Store will give you error reports with line numbers and you don't want to wonder what part of the line caused the error.

I'm pretty sure your filename is not what you expect if that is an exact copy of the code.

CAMERA_PIC_DIR = "/DCIM/Camera/blocks";
ImageDir = Environment.getExternalStorageDirectory().getAbsolutePath() + CAMERA_PIC_DIR;
Bitmap WBL_reloaded = BitmapFactory.decodeFile(ImageDir+"WBL_a10.jpg");
That gives: .../DCIM/Camera/blocksWBL_a10.jpg
I'm guessing you forgot a forward slash on the CAMERA_PIC_DIR line.

I'm guessing you forgot a forward slash on the CAMERA_PIC_DIR line.

That fixed it!!!!!

So simple yet so subtle to spot (by me at least)!!

My weary mind gave up

Thanks frob

can't help being grumpy...

Just need to let some steam out, so my head doesn't explode...

This topic is closed to new replies.

Advertisement