如何从画廊的图像的图像格式 [英] How to get the Image Format of the images from Gallery

查看:120
本文介绍了如何从画廊的图像的图像格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道的图像格式(即JPFG / PNG等)和我从画廊得到的图像。

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

我需要的是从设备的画廊PIC图像和它Base64格式发送给服务器,服务器想知道的图像格式。

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.

任何帮助是AP preciated。

Any help is appreciated.

推荐答案

使用下面的方法来检索库的MIME类型的图像:

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;
}

这将返回类似图像/ JPEG。

This will return something like "image/jpeg".

您可以使用以下code将图像从画廊转换成你想要的格式,如JPG:

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

这样,您将控制您发送到服务器的图像格式,甚至可以融为一体preSS更节省带宽。 ;)

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

这篇关于如何从画廊的图像的图像格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆