1

Given a file path (without extension) I'd like to know if the image inside the file is a JPEG or a PNG.

How can I do it?

rene
  • 41,474
  • 78
  • 114
  • 152
Addev
  • 31,819
  • 51
  • 183
  • 302

3 Answers3

8

Try looking for the image headers, somewhat like this:

File file = /* (get your file) */;
byte[] data = new byte[2];
try {
    new FileInputStream(file).read(data);
} catch (Exception e) {
    // handle the error somehow!
}

if (data[0] == 0xFF && data[1] == 0xD8) {
    // jpeg
} else if (data[0] == 0x89 && data[1] == 0x50) {
    // png
} else {
    // error?
}

JPEG headers will always be FFD8, and PNG headers are 89504E470D0A1E0A (for which we only need to look at the first two bytes to distinguish from JPEG).

tckmn
  • 57,719
  • 27
  • 114
  • 156
  • Also shed light on other types of image file format. A link is appreciated. – Ravinder Reddy Mar 31 '14 at 12:47
  • 1
    @Ravinder that is not in the question but it could be a new question. However you could google your self to find references to image file formats and research what their first and second byte are. If your expanded code based on this answer gives issues for one or more filetypes you can ask a question yourself. – rene Mar 31 '14 at 12:55
  • @Rene: Ridiculous. SO says [*Sharing your research helps everyone*](http://stackoverflow.com/questions/how-to-ask). – Ravinder Reddy Mar 31 '14 at 13:09
  • Doorknob did share the links he used to answer the question. Why do you expect more? – rene Mar 31 '14 at 13:13
  • 1
    @Ravinder that's under "How to ask". This is an answer. You can do your own research and post new answer that covers all existing image formats. Good luck! – Shadow The GPT Wizard Mar 31 '14 at 13:20
  • @rene: *Why do you expect more?*. Well, to get shared if one has already found a resource that missed by others for reasons. And most of answers on SO share other information beyond what the OP is looking as a solution for his posted problem. – Ravinder Reddy Mar 31 '14 at 13:26
  • @ShadowWizard: It was not that I don't know how to search. Please look at my just previous comment of this. – Ravinder Reddy Mar 31 '14 at 13:27
  • @Ravinder I see. We disagree. – rene Mar 31 '14 at 13:29
  • @Ravinder I did and I truly don't know what you want. Question is about JPG or PNG only. Answer is full and with details and with links. If you don't like it and think it's missing (?!) downvote and move on, but I honestly don't see why you think this way. – Shadow The GPT Wizard Mar 31 '14 at 13:29
  • @ShadowWizard: I did not say that the answer is incomplete. I felt that it fulfilled the OP's requirements and thought OA may be knowing something beyond it. And hence, I made a suggestion to include a side note on other formats if possible and if knew a resource on line. – Ravinder Reddy Mar 31 '14 at 13:34
  • @Ravinder OK, if the author of the answer here wants to he can do it of course, but I don't think it's really necessary. – Shadow The GPT Wizard Mar 31 '14 at 13:35
2

You can find it like this:

step 1: You just get the image bounds only....

step 2: You can change your image with your corresponding size using the below method

    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    in.mark(in.available());
    BitmapFactory.decodeStream(in, null, options);
    (or)
    BitmapFactory.decodeFile(pathName);
    (or)
    BitmapFactory.decodeResource(MainActivity.this.getResources(), R.drawable.ic_launcher);
    String name = options.outMimeType;

this below method is used to get the large bitmaps loading with efficiently re-sizing the image with your required height and width

public static Bitmap decodeSampledBitmapFromResource(InputStream in,
            int reqWidth, int reqHeight) throws IOException {

        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        in.mark(in.available());
        BitmapFactory.decodeStream(in, null, options);
        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth,
                reqHeight);
        in.reset();
        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeStream(in, null, options);
    }

    public static int calculateInSampleSize(BitmapFactory.Options options,
            int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {
            final int halfHeight = height / 2;
            final int halfWidth = width / 2;
            // Calculate the largest inSampleSize value that is a power of 2 and
            // keeps both
            // height and width larger than the requested height and width.
            while ((halfHeight / inSampleSize) > reqHeight
                    && (halfWidth / inSampleSize) > reqWidth) {
                inSampleSize *= 2;
            }
        }

        return inSampleSize;
    }
Naveen Kumar Kuppan
  • 1,424
  • 1
  • 10
  • 12
1

Do you search for this? Why don't you have the extension?

"In Java 7 you can now just use

 Files.probeContentType(path)"

-> source but we are not sure if it's supported in android.

So why don't you just get the full path and extract the file extension with substring?

Community
  • 1
  • 1
malle
  • 334
  • 4
  • 17
  • Link-only answers [are discouraged](http://meta.stackexchange.com/q/8231/191399). This should probably be a comment instead. – Keppil Mar 31 '14 at 12:19
  • It is still just basically a copy of the SO answer you link to, so it is not much better. – Keppil Mar 31 '14 at 12:22
  • @fge you might be right i couldn't found anything official about java 7 support for android, but it might be worth a try. – malle Mar 31 '14 at 12:26
  • I believe the point of the question is that there is no file extension available. – user247702 Mar 31 '14 at 12:59