Android 如何从Gallery中获取图像的图像格式

Android 如何从Gallery中获取图像的图像格式,android,gallery,photo-gallery,Android,Gallery,Photo Gallery,我想知道的图像格式(即JPFG/PNG等),我从画廊得到的图像 我的需要是从设备库中拍摄图像,并将其以Base64格式发送到服务器,但服务器希望知道图像格式 非常感谢您的帮助。您可以通过以下操作从文件路径获取文件扩展名: int dotposition= filePath.lastIndexOf("."); String format = filePath.substring(dotposition + 1, file.length()); 这能解决问题吗?编辑: 使用以下方法从库中检索图像的

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

我的需要是从设备库中拍摄图像,并将其以Base64格式发送到服务器,但服务器希望知道图像格式


非常感谢您的帮助。

您可以通过以下操作从文件路径获取文件扩展名:

int dotposition= filePath.lastIndexOf(".");
String format = filePath.substring(dotposition + 1, file.length());
这能解决问题吗?

编辑: 使用以下方法从库中检索图像的MIME类型:

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;
}
这将返回类似“image/jpeg”的内容


先前的答复: 您可以使用以下代码将Gallery中的图像转换为所需的格式,如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...
}

通过这种方式,您可以控制发送到服务器的图像格式,甚至可以压缩更多图像以节省带宽。;)

可以仅使用路径获取MIME类型

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

图像是否发送到webserver@VivekKhandelwal是的,图像被发送到服务器。感谢您给时间解决这个问题,但转换是不同的情况。我需要知道如何获得文件的MIME类型,因为它可以在iOS中完成。有没有任何方法可以实现无上下文依赖性?