在我們的應用程序的用戶已經上傳百萬計的圖像多年來(大約)驗證碼:安卓:上傳圖片,而無需使用丟失的Exif數據
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();
}
}
..但有例外java.io.IOException: ExifInterface does not support saving attributes for the current input.
newExif.saveAttributes();
後,因爲我試圖將屬性保存到InputStream。我還能怎麼做?
的可能的複製[如何保存的Exif數據中之後的Android位圖壓縮](https://stackoverflow.com/questions/23762133/how-to-save-exif-data-位圖壓縮後在Android) – amuttsch
我見過這個。經過建議之後,但由於我寫了'ExifInterface'只能保存到圖像,這是我的問題,所以它不是重複的。 – Ambran
我沒有看到你的問題。爲原始文件和壓縮文件創建一個'ExifInterface'(從輸出流創建一個新的'Bitmap')並使用'exifComp.setAttribute(TAG _...,exifOrig(TAG _...));'並保存它用'exifComp.save()'。之後,從壓縮文件中獲取輸出流。 – amuttsch