Search code examples
androidbitmapfactory

decodeFile returns null Bitmap for an existing file


I have tried all the solutions I could find on stackoverflow to resolve this issue, but in vain. In running the following code, mAlbumPhotoUri is "/mnt/sdcard/photo/1342147146535.jpg" which is Uri type. file.exists() indicates this file exists, but resultBitmap is null, after executing the last line of code.

What am I doing wrong?

File file = new File(mAlbumPhotoUri.toString());
if(file.exists()){
   Toast.makeText(this, "File exists in /mnt", Toast.LENGTH_LONG);}
else {
Toast.makeText(this, "File NOT exists in /mnt", Toast.LENGTH_LONG);}

BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true; //only get the size of the bitmap                

if (resultPhotoBitmap != null) {
    resultPhotoBitmap.recycle();
    }
String fname=new File(mAlbumPhotoUri.toString()).getAbsolutePath();
resultPhotoBitmap = BitmapFactory.decodeFile(fname, options);

Solution

  • Adding the option inJustDecodeBounds to your BitmapFactory does just that; it only decodes the size of the Bitmap and loads that data back into the options object's outHeight and outWidth. It does not decode the actual Bitmap and return it to you.

    If you want to actually get the Bitmap itself, remove that option and call decodeFile() again.