Using images saved in drawable in an app - Android

71 views Asked by At

I'm fairly new to android and was asked to improve an app. The app uses images which are saved in a file in the phone. I added a feature where the app also uses images that are saved in the drawable folder. However, the following code is not working now:

public static int[] getImageSize(Uri uri) {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(uri.getPath(), options);
        return new int[] { options.outWidth, options.outHeight };
    }

Which returns the size of the image. I'm pretty sure it's not working because of the decodeFile() method. How can I fix this to make it work with my drawables too?

1

There are 1 answers

0
Tyler Kiser On

You will need to check if the image is a bitmap from the file, or if it is a drawable first. Make a new function to get the drawable size with this code

public static int[] getImageSize(Drawable image) {
    int iHeight = image.getIntrinsicHeight(); 
    int iWidth = image.getIntrinsicWidth(); 
    return new int[] { iWidth , iHeight  };
}