Вопрос

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?

Это было полезно?

Решение 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;
    }

Другие советы

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).

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?

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top