【发布时间】:2017-07-05 13:47:34
【问题描述】:
在我们的应用中,用户多年来一直使用(大致)以下代码上传数百万张图片:
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(postFilePath, bmOptions);
Bitmap roughBitmap = BitmapFactory.decodeFile(postFilePath, bmOptions);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
roughBitmap.compress(Bitmap.CompressFormat.JPEG, 70, stream);
InputStream fis = new ByteArrayInputStream(stream.toByteArray());
int fileSize = stream.toByteArray().length;
conn.setRequestProperty("Content-Length", Integer.toString(fileSize));
conn.setFixedLengthStreamingMode(fileSize);
...
if (fis != null) {
byte[] buf = new byte[10240];
int read;
while ((read = fis.read(buf)) > 0) {
os.write(buf, 0, read);
totalBytesRead += read;
if (uploadProgressListener != null) {
try {
uploadProgressListener.onBytesUploaded(read);
} catch (Exception e) {
Log.e(e);
}
}
}
fis.close();
}
最近我们看到需要保留上传图片的Exif 数据。问题是压缩位图时图像Exif数据丢失。我想过使用ExifInterface 从原始文件中提取这些数据:
ExifInterface oldExif = new ExifInterface(postFilePath);
String value = oldExif.getAttribute(ExifInterface.TAG_DATETIME);
..然后将其添加到 InputStream fis 然后继续上传文件。问题是ExifInterface 无法将 Exif 数据保存到 InputStream。
图片上传到服务器后,Exif 数据如何保留?
不是重复的:
为了更深入地澄清,我尝试使用建议的duplicate question 使用此方法:
public static void copyExif(String originalPath, InputStream newStream) throws IOException {
String[] attributes = new String[]
{
ExifInterface.TAG_DATETIME,
ExifInterface.TAG_DATETIME_DIGITIZED,
ExifInterface.TAG_EXPOSURE_TIME,
ExifInterface.TAG_FLASH,
ExifInterface.TAG_FOCAL_LENGTH,
ExifInterface.TAG_GPS_ALTITUDE,
ExifInterface.TAG_GPS_ALTITUDE_REF,
ExifInterface.TAG_GPS_DATESTAMP,
ExifInterface.TAG_GPS_LATITUDE,
ExifInterface.TAG_GPS_LATITUDE_REF,
ExifInterface.TAG_GPS_LONGITUDE,
ExifInterface.TAG_GPS_LONGITUDE_REF,
ExifInterface.TAG_GPS_PROCESSING_METHOD,
ExifInterface.TAG_GPS_TIMESTAMP,
ExifInterface.TAG_MAKE,
ExifInterface.TAG_MODEL,
ExifInterface.TAG_ORIENTATION,
ExifInterface.TAG_SUBSEC_TIME,
ExifInterface.TAG_WHITE_BALANCE
};
ExifInterface oldExif = new ExifInterface(originalPath);
ExifInterface newExif = new ExifInterface(newStream);
if (attributes.length > 0) {
for (int i = 0; i < attributes.length; i++) {
String value = oldExif.getAttribute(attributes[i]);
if (value != null)
newExif.setAttribute(attributes[i], value);
}
newExif.saveAttributes();
}
}
.. 但在newExif.saveAttributes(); 之后出现异常java.io.IOException: ExifInterface does not support saving attributes for the current input.,因为我正在尝试将属性保存到 InputStream。我还能怎么做?
【问题讨论】:
-
我看过这个。遵循建议,但正如我写的
ExifInterface只能保存到图像,这是我的问题,所以它不是重复的。 -
我没有看到你的问题。为原始文件和压缩文件创建一个
ExifInterface(从输出流创建一个新的Bitmap)并使用exifComp.setAttribute(TAG_..., exifOrig(TAG_...));并使用exifComp.save()保存它。然后,从压缩文件中获取输出流。 -
@amuttsch,您的意思是先保存压缩文件,然后将exif属性保存到其中,然后再次从中读取流吗?我可以这样做,但希望避免在此过程中将压缩文件保存到存储中。
-
您可以从输出流中获取输入流并使用
BitmapFactory,参见:stackoverflow.com/questions/29286599/…