简体   繁体   中英

Using images saved in drawable in an app - Android

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?

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  };
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM