Vin*_*kla 9 android gallery photo-gallery
我想知道我从图库中获取的图像的图像格式(即JPFG/PNG等).
我需要从设备的图库中拍摄图像并以Base64格式将其发送到服务器,但服务器想知道图像格式.
任何帮助表示赞赏.
使用以下方法从库中检索图像的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;
}
Run Code Online (Sandbox Code Playgroud)
这将返回类似"image/jpeg"的内容.
您可以使用以下代码将图像从库转换为您想要的格式,如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...
}
Run Code Online (Sandbox Code Playgroud)
这样您就可以控制发送到服务器的图像格式,甚至可以压缩更多以节省带宽.;)
您可以通过执行以下操作从文件路径获取文件扩展名:
int dotposition= filePath.lastIndexOf(".");
String format = filePath.substring(dotposition + 1, file.length());
Run Code Online (Sandbox Code Playgroud)
这能解决问题吗?
| 归档时间: |
|
| 查看次数: |
8050 次 |
| 最近记录: |