Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get mime type from a bitmap object in android?

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

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

Now I want to cross check the format, outBounds and memory space size. But I don't wanna do a lengthy or a tedious task eg. to convert it to back to stream and then convert it again to BItmap using BitmapFactory with options.

like image 676
Vineet Avatar asked Jan 25 '13 10:01

Vineet


1 Answers

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

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

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

like image 61
Ilmari Karonen Avatar answered Sep 18 '22 22:09

Ilmari Karonen