How to get the Image Format of the images from Gallery
Asked Answered
B

4

12

I want to know the Image format (i.e. JPFG/PNG etc) of the images which I am getting from the gallery.

My need is to pic images from the gallery of the device and send it to server in Base64 format but the server wants to know image format.

Any help is appreciated.

Briton answered 23/2, 2012 at 13:59 Comment(2)
is the image send to webserverMulry
@Vivek Khandelwal Yes the image is send to the server.Briton
B
11

Edited:

Use the following method to retrieve the MIME type of an image from the gallery:

public static String GetMimeType(Context context, Uri uriImage)
{
    String strMimeType = null;

    Cursor cursor = context.getContentResolver().query(uriImage,
                        new String[] { MediaStore.MediaColumns.MIME_TYPE },
                        null, null, null);

    if (cursor != null && cursor.moveToNext())
    {
        strMimeType = cursor.getString(0);
    }

    return strMimeType;
}

This will return something like "image/jpeg".


Previous answer:

You can use the following code to convert the image from the Gallery to the format you want, like a JPG:

ByteArrayOutputStream outputBuffer = new ByteArrayOutputStream();

Bitmap bitmapImage = BitmapFactory.decodeStream(
                         getContentResolver().openInputStream(myImageUri));

if (bitmapImage.compress(Bitmap.CompressFormat.JPEG, 100, outputBuffer))
{
    // Then perform a base64 of the byte array...
}

This way you will control the image format your are sending to the server, and can even compress more to save bandwidth. ;)

Bobsledding answered 17/4, 2012 at 16:9 Comment(2)
Thanks for giving time to the problem but conversion is different scenario. I need to know how to get the MIME type of the file as it can be done in iOS.Briton
Is there any way to achieve without context dependency?Waiwaif
W
3

It is possible to get MIME type with path alone.

 public static String getMimeType(String imageUrl) {
        String extension = MimeTypeMap.getFileExtensionFromUrl(imageUrl);
        String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(
                extension);
        // Do Manual manipulation if needed
        if ("image/x-ms-bmp".equals(mimeType)) {
            mimeType = "image/bmp";
        }
        return mimeType;
    }
Waiwaif answered 15/3, 2016 at 5:30 Comment(0)
P
0

You can get the file extension from the file path by doing something like this:

int dotposition= filePath.lastIndexOf(".");
String format = filePath.substring(dotposition + 1, file.length());

Does that fix the issue?

Preshrunk answered 23/2, 2012 at 14:28 Comment(0)
D
0

You can use built-in Android image decoder BitmapFactory.

If you deal with a File you can use this KTX function:

fun File.getImageMimeType(): String? {
    if (!exists()) {
        return null
    }
    val options = BitmapFactory.Options()
    options.inJustDecodeBounds = true
    BitmapFactory.decodeFile(path, options)
    return options.outMimeType
}

If you deal with a Uri you can use this KTX function:

fun Uri.getImageMimeType(context: Context): String? {
    val options = BitmapFactory.Options()
    options.inJustDecodeBounds = true
    context.contentResolver.openInputStream(this)?.use {
        BitmapFactory.decodeStream(it, null, options)
    }
    return options.outMimeType
}
Denticle answered 19/7, 2022 at 22:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.