Android:上传JPG图像而不会丢失EXIF
问题描述:
我将JPG图像上传为byte [],但是在转换为byte []之前EXIF的Bitmap条带。如何上传原始jpg而不将其转换为位图?Android:上传JPG图像而不会丢失EXIF
File imagefile = new File(filepath + "DSC00021.jpg");
FileInputStream fis = new FileInputStream(imagefile);
Bitmap bi = BitmapFactory.decodeStream(fis); // EXIF info lost
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bi.compress(Bitmap.CompressFormat.JPG, 100, baos);
byte[] data = baos.toByteArray();
p.s.我不想使用任何第三方库。 ExifInterface只能写入文件而不能写入流/字节数组。
答
将文件转换为位图由
Bitmap bi = BitmapFactory.decode(filepath + "DSC00021.jpg");
可以过指定选项,看看API documentation
或者,如果你想从一个文件到另一个交换的元数据,sanselan可能会是最佳选择。这在处理图像时会很有帮助,例如重新调整大小。
sample code将引导你在一个正确的方向。
+0
我提到过,我不想用第三方库来做。 – Taranfx 2012-07-20 15:10:24
答
以上代码不适用于大多数情况。如果你想解码大尺寸图像,你会得到“内存不足错误”。使用bitmpafactory选项解码。
为什么不把整个文件内容从FileInputStream读到字节数组而不是转换成Bitmap对象?谷歌搜索会给你吨结果如何获得一个文件的内容到一个字节数组。 – 2011-12-20 05:17:25