androidexceptionbitmapfactory

Catching BitmapFactory exceptions


I am simply trying to load a bitmap from a file using the BitmapFactory class as shown:

Bitmap b = BitmapFactory.decodeFile(getActivity().getFilesDir() + "/myImage.jpg");

This works fine except in instances where the file does not exist on filesystem and it throws a java.io.FileNotFoundException (No such file or directory). My question is, how do I catch this exception and keep going? I have surrounded the statement in a try block but the catch statement says it will never be reached for any FileNotFoundException. Taking a look at the BitmapFactory class shows that it doesn't throw any exceptions. What am I doing wrong here?


Solution

  • Try to explicitly define the right Exception

    try{
        Bitmap b = BitmapFactory.decodeFile(getActivity().getFilesDir() + "/myImage.jpg");
    }catch(java.io.FileNotFoundException ex){
       //Caught exception
    }