Search code examples
androidbitmapjpegbitmapfactory

Resolution of jpg file


I have a class where in constructor I get a file. This file is jpeg. How I can get resolution this jpeg file in this class? This is some code from constructor:

public static Bitmap bitmapSizer(File file) {

        BitmapFactory.Options options = new BitmapFactory.Options();
        Bitmap bitmap = null;

        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(file.getAbsolutePath(), options);

        int imageHeight = options.outHeight;
        int imageWidth = options.outWidth;
        options.inDither = true;
        options.inPreferredConfig = Bitmap.Config.ARGB_4444;
        options.inPurgeable = true;
        options.inSampleSize=8;         
        options.inJustDecodeBounds = false;

Solution

  • You need to move a few lines of code.
    First, get the Options object up:

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inDither = true;
        options.inPreferredConfig = Bitmap.Config.ARGB_4444;
        options.inPurgeable = true;
        options.inSampleSize=8;         
        options.inJustDecodeBounds = true;
    

    Note the options.inJustDecodeBounds =true This will only read jpg's header, not the whole image.
    Next, decode your file:

        Bitmap bitmap = null;
        BitmapFactory.decodeFile(file.getAbsolutePath(), options);
    

    After decoding you'll get the results in:

        int imageHeight = options.outHeight;
        int imageWidth = options.outWidth;