From d065a828da8c659f49833d7dbc08c63b9a82827b Mon Sep 17 00:00:00 2001 From: Ray Essick Date: Wed, 7 Jun 2023 11:16:55 +0000 Subject: [PATCH 1/2] Revert "Use a ExifInterface ctor with a file descriptor as a parameter" This reverts commit e106aa909b2aafb12644a695cf4e1d36a8e1f60e. Reason for revert: OEM reports regressions Bug: 199822700 Bug: 283908789 (cherry picked from https://android-review.googlesource.com/q/commit:2f9e9118ff556bca2e9651f05a821c8e570fbe52) Merged-In: Ic00be58b718be5e53823d81fa64cb718a87ed05a Change-Id: Ic00be58b718be5e53823d81fa64cb718a87ed05a --- media/java/android/media/ThumbnailUtils.java | 25 +++++++++----------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/media/java/android/media/ThumbnailUtils.java b/media/java/android/media/ThumbnailUtils.java index 6744359d12d70..9b238e1722475 100644 --- a/media/java/android/media/ThumbnailUtils.java +++ b/media/java/android/media/ThumbnailUtils.java @@ -49,7 +49,6 @@ import com.android.internal.util.ArrayUtils; import libcore.io.IoUtils; import java.io.File; -import java.io.FileInputStream; import java.io.IOException; import java.util.Arrays; import java.util.Comparator; @@ -256,19 +255,17 @@ public class ThumbnailUtils { // get orientation if (MediaFile.isExifMimeType(mimeType)) { - try (FileInputStream is = new FileInputStream(file)) { - exif = new ExifInterface(is.getFD()); - switch (exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 0)) { - case ExifInterface.ORIENTATION_ROTATE_90: - orientation = 90; - break; - case ExifInterface.ORIENTATION_ROTATE_180: - orientation = 180; - break; - case ExifInterface.ORIENTATION_ROTATE_270: - orientation = 270; - break; - } + exif = new ExifInterface(file); + switch (exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 0)) { + case ExifInterface.ORIENTATION_ROTATE_90: + orientation = 90; + break; + case ExifInterface.ORIENTATION_ROTATE_180: + orientation = 180; + break; + case ExifInterface.ORIENTATION_ROTATE_270: + orientation = 270; + break; } } From 77df079ce87849781d43c5705a6ff61d8f9b236d Mon Sep 17 00:00:00 2001 From: Ray Essick Date: Wed, 7 Jun 2023 11:17:16 +0000 Subject: [PATCH 2/2] Revert "ExifInterface: Use FileDescriptors whenever we can." This reverts commit e69a12f356ada49ae0f99ed3810f36776c25f3d8. Reason for revert: OEM reports regressions Bug: 199822700 Bug: 283908789 (cherry picked from https://android-review.googlesource.com/q/commit:c938e5974ee2fa7c75bccde3952e271f82a7605f) Merged-In: Icd62dc912938b01015b902d2a072f3b758e6084a Change-Id: Icd62dc912938b01015b902d2a072f3b758e6084a --- media/java/android/media/ExifInterface.java | 109 +++++++++----------- 1 file changed, 51 insertions(+), 58 deletions(-) diff --git a/media/java/android/media/ExifInterface.java b/media/java/android/media/ExifInterface.java index f86b9af25933b..23f87abaffed8 100644 --- a/media/java/android/media/ExifInterface.java +++ b/media/java/android/media/ExifInterface.java @@ -1566,7 +1566,7 @@ public class ExifInterface { FileInputStream in = null; try { in = new FileInputStream(fileDescriptor); - loadAttributes(in, fileDescriptor); + loadAttributes(in); } finally { closeQuietly(in); if (isFdDuped) { @@ -1637,7 +1637,7 @@ public class ExifInterface { mSeekableFileDescriptor = null; } } - loadAttributes(inputStream, null); + loadAttributes(inputStream); } /** @@ -1963,7 +1963,7 @@ public class ExifInterface { * This function decides which parser to read the image data according to the given input stream * type and the content of the input stream. */ - private void loadAttributes(@NonNull InputStream in, @Nullable FileDescriptor fd) { + private void loadAttributes(@NonNull InputStream in) { if (in == null) { throw new NullPointerException("inputstream shouldn't be null"); } @@ -1993,7 +1993,7 @@ public class ExifInterface { break; } case IMAGE_TYPE_HEIF: { - getHeifAttributes(inputStream, fd); + getHeifAttributes(inputStream); break; } case IMAGE_TYPE_ORF: { @@ -2580,7 +2580,7 @@ public class ExifInterface { } else if (isSeekableFD(in.getFD())) { mSeekableFileDescriptor = in.getFD(); } - loadAttributes(in, null); + loadAttributes(in); } finally { closeQuietly(in); if (modernFd != null) { @@ -3068,66 +3068,59 @@ public class ExifInterface { } } - private void getHeifAttributes(ByteOrderedDataInputStream in, @Nullable FileDescriptor fd) - throws IOException { + private void getHeifAttributes(ByteOrderedDataInputStream in) throws IOException { MediaMetadataRetriever retriever = new MediaMetadataRetriever(); try { - if (fd != null) { - retriever.setDataSource(fd); - } else { - retriever.setDataSource(new MediaDataSource() { - long mPosition; + retriever.setDataSource(new MediaDataSource() { + long mPosition; - @Override - public void close() throws IOException {} + @Override + public void close() throws IOException {} - @Override - public int readAt(long position, byte[] buffer, int offset, int size) - throws IOException { - if (size == 0) { - return 0; - } - if (position < 0) { - return -1; - } - try { - if (mPosition != position) { - // We don't allow seek to positions after the available bytes, - // the input stream won't be able to seek back then. - // However, if we hit an exception before (mPosition set to -1), - // let it try the seek in hope it might recover. - if (mPosition >= 0 && position >= mPosition + in.available()) { - return -1; - } - in.seek(position); - mPosition = position; - } - - // If the read will cause us to go over the available bytes, - // reduce the size so that we stay in the available range. - // Otherwise the input stream may not be able to seek back. - if (size > in.available()) { - size = in.available(); - } - - int bytesRead = in.read(buffer, offset, size); - if (bytesRead >= 0) { - mPosition += bytesRead; - return bytesRead; - } - } catch (IOException e) { - // absorb the exception and fall through to the 'failed read' path below - } - mPosition = -1; // need to seek on next read + @Override + public int readAt(long position, byte[] buffer, int offset, int size) + throws IOException { + if (size == 0) { + return 0; + } + if (position < 0) { return -1; } + try { + if (mPosition != position) { + // We don't allow seek to positions after the available bytes, + // the input stream won't be able to seek back then. + // However, if we hit an exception before (mPosition set to -1), + // let it try the seek in hope it might recover. + if (mPosition >= 0 && position >= mPosition + in.available()) { + return -1; + } + in.seek(position); + mPosition = position; + } - @Override - public long getSize() throws IOException { - return -1; - } - }); - } + // If the read will cause us to go over the available bytes, + // reduce the size so that we stay in the available range. + // Otherwise the input stream may not be able to seek back. + if (size > in.available()) { + size = in.available(); + } + + int bytesRead = in.read(buffer, offset, size); + if (bytesRead >= 0) { + mPosition += bytesRead; + return bytesRead; + } + } catch (IOException e) {} + mPosition = -1; // need to seek on next read + return -1; + } + + @Override + public long getSize() throws IOException { + return -1; + } + }); String exifOffsetStr = retriever.extractMetadata( MediaMetadataRetriever.METADATA_KEY_EXIF_OFFSET);