How to get mime type from bitmap object in android?

I want to get the mime type of a bitmap object. Actually I have compressed the bitmap using

bitmap.compress (Bitmap.CompressFormat.PNG , 100, stream);

      

Now I want to cross check the format, outBounds and memory size. But I don't want to do a long or tedious task, for example. to convert it back to a stream and then convert it back to BItmap using BitmapFactory with parameters.

+3


source to share


1 answer


According to Dmitry, if you have a compressed image in the stream, then you need to decode it using BitmapFactory to tell what type it is. (Technically, you can also read the stream yourself and run your own ad hoc file format checks on it, but I'm guessing you'd prefer to use existing tools rather than collapse your own.)

One optimization you can and should do is set BitmapFactory.Options.inJustDecodeBounds to avoid unnecessary decoding of the entire image if you just want to know what type it is. After decoding, the outMimeType property should indicate the type of the image.



As for the literal question in your title, it doesn't make sense. A Bitmap doesn't have a MIME type - it's just a container for an array of pixels. You can compress the bitmap to stream using various formats like JPEG, PNG, or WEBP, but that just gives you a compressed copy of the image.

+2


source







All Articles