I want to know the Image format (i.e. JPFG/PNG etc) of the images which I am getting from the gallery.
我想知道图片的格式(例如JPFG/PNG等),我正在从图片库中获取。
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.
我需要从设备的图片库中获取图片,并以Base64格式发送给服务器,但是服务器想要知道图像格式。
Any help is appreciated.
任何帮助都是感激。
3 个解决方案
#1
8
Edited:
Use the following method to retrieve the MIME type of an image from the gallery:
使用以下方法从图库检索图像的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;
}
This will return something like "image/jpeg".
这将返回“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:
您可以使用以下代码将图像从图库转换为您想要的格式,如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. ;)
这样,您就可以控制发送到服务器的图像格式,甚至可以压缩更多以节省带宽。,)
#2
1
It is possible to get MIME type with path alone.
只有路径才能获得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;
}
#3
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?
这能解决问题吗?
#1
8
Edited:
Use the following method to retrieve the MIME type of an image from the gallery:
使用以下方法从图库检索图像的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;
}
This will return something like "image/jpeg".
这将返回“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:
您可以使用以下代码将图像从图库转换为您想要的格式,如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. ;)
这样,您就可以控制发送到服务器的图像格式,甚至可以压缩更多以节省带宽。,)
#2
1
It is possible to get MIME type with path alone.
只有路径才能获得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;
}
#3
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?
这能解决问题吗?