1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.documentsui.archives; 18 19 import android.media.ExifInterface; 20 import android.os.Bundle; 21 import android.provider.DocumentsContract; 22 23 import androidx.annotation.Nullable; 24 25 import java.io.IOException; 26 import java.io.InputStream; 27 import java.util.ArrayList; 28 import java.util.HashMap; 29 import java.util.List; 30 import java.util.Map; 31 32 /** 33 * Class providing support for extracting metadata from a file as a 34 * {@link Bundle} suitable for use with {@link DocumentsContract#getDocumentMetadata}. 35 * <p>Currently only EXIF data is supported. 36 * <p>TODO: Add support for common video and audio types, as well as PDF files. 37 * Copied from android.provider.MetadataReader.java 38 */ 39 public final class MetadataReader { 40 MetadataReader()41 private MetadataReader() {} 42 43 private static final String[] DEFAULT_EXIF_TAGS = { 44 ExifInterface.TAG_APERTURE, 45 ExifInterface.TAG_COPYRIGHT, 46 ExifInterface.TAG_DATETIME, 47 ExifInterface.TAG_EXPOSURE_TIME, 48 ExifInterface.TAG_FOCAL_LENGTH, 49 ExifInterface.TAG_F_NUMBER, 50 ExifInterface.TAG_GPS_LATITUDE, 51 ExifInterface.TAG_GPS_LATITUDE_REF, 52 ExifInterface.TAG_GPS_LONGITUDE, 53 ExifInterface.TAG_GPS_LONGITUDE_REF, 54 ExifInterface.TAG_IMAGE_LENGTH, 55 ExifInterface.TAG_IMAGE_WIDTH, 56 ExifInterface.TAG_ISO_SPEED_RATINGS, 57 ExifInterface.TAG_MAKE, 58 ExifInterface.TAG_MODEL, 59 ExifInterface.TAG_ORIENTATION, 60 ExifInterface.TAG_SHUTTER_SPEED_VALUE, 61 }; 62 63 private static final int TYPE_INT = 0; 64 private static final int TYPE_DOUBLE = 1; 65 private static final int TYPE_STRING = 2; 66 67 private static final Map<String, Integer> TYPE_MAPPING = new HashMap<>(); 68 static { 69 // TODO: Move this over to ExifInterface.java 70 // Since each ExifInterface item has a type, and there's currently no way to get the type 71 // from the tag, here we identify the tag to the type so that we can call the correct 72 // ExifInterface method TYPE_MAPPING.put(ExifInterface.TAG_ARTIST, TYPE_STRING)73 TYPE_MAPPING.put(ExifInterface.TAG_ARTIST, TYPE_STRING); TYPE_MAPPING.put(ExifInterface.TAG_BITS_PER_SAMPLE, TYPE_INT)74 TYPE_MAPPING.put(ExifInterface.TAG_BITS_PER_SAMPLE, TYPE_INT); TYPE_MAPPING.put(ExifInterface.TAG_COMPRESSION, TYPE_INT)75 TYPE_MAPPING.put(ExifInterface.TAG_COMPRESSION, TYPE_INT); TYPE_MAPPING.put(ExifInterface.TAG_COPYRIGHT, TYPE_STRING)76 TYPE_MAPPING.put(ExifInterface.TAG_COPYRIGHT, TYPE_STRING); TYPE_MAPPING.put(ExifInterface.TAG_DATETIME, TYPE_STRING)77 TYPE_MAPPING.put(ExifInterface.TAG_DATETIME, TYPE_STRING); TYPE_MAPPING.put(ExifInterface.TAG_IMAGE_DESCRIPTION, TYPE_STRING)78 TYPE_MAPPING.put(ExifInterface.TAG_IMAGE_DESCRIPTION, TYPE_STRING); TYPE_MAPPING.put(ExifInterface.TAG_IMAGE_LENGTH, TYPE_INT)79 TYPE_MAPPING.put(ExifInterface.TAG_IMAGE_LENGTH, TYPE_INT); TYPE_MAPPING.put(ExifInterface.TAG_IMAGE_WIDTH, TYPE_INT)80 TYPE_MAPPING.put(ExifInterface.TAG_IMAGE_WIDTH, TYPE_INT); TYPE_MAPPING.put(ExifInterface.TAG_JPEG_INTERCHANGE_FORMAT, TYPE_INT)81 TYPE_MAPPING.put(ExifInterface.TAG_JPEG_INTERCHANGE_FORMAT, TYPE_INT); TYPE_MAPPING.put(ExifInterface.TAG_JPEG_INTERCHANGE_FORMAT_LENGTH, TYPE_INT)82 TYPE_MAPPING.put(ExifInterface.TAG_JPEG_INTERCHANGE_FORMAT_LENGTH, TYPE_INT); TYPE_MAPPING.put(ExifInterface.TAG_MAKE, TYPE_STRING)83 TYPE_MAPPING.put(ExifInterface.TAG_MAKE, TYPE_STRING); TYPE_MAPPING.put(ExifInterface.TAG_MODEL, TYPE_STRING)84 TYPE_MAPPING.put(ExifInterface.TAG_MODEL, TYPE_STRING); TYPE_MAPPING.put(ExifInterface.TAG_ORIENTATION, TYPE_INT)85 TYPE_MAPPING.put(ExifInterface.TAG_ORIENTATION, TYPE_INT); TYPE_MAPPING.put(ExifInterface.TAG_PHOTOMETRIC_INTERPRETATION, TYPE_INT)86 TYPE_MAPPING.put(ExifInterface.TAG_PHOTOMETRIC_INTERPRETATION, TYPE_INT); TYPE_MAPPING.put(ExifInterface.TAG_PLANAR_CONFIGURATION, TYPE_INT)87 TYPE_MAPPING.put(ExifInterface.TAG_PLANAR_CONFIGURATION, TYPE_INT); TYPE_MAPPING.put(ExifInterface.TAG_PRIMARY_CHROMATICITIES, TYPE_DOUBLE)88 TYPE_MAPPING.put(ExifInterface.TAG_PRIMARY_CHROMATICITIES, TYPE_DOUBLE); TYPE_MAPPING.put(ExifInterface.TAG_REFERENCE_BLACK_WHITE, TYPE_DOUBLE)89 TYPE_MAPPING.put(ExifInterface.TAG_REFERENCE_BLACK_WHITE, TYPE_DOUBLE); TYPE_MAPPING.put(ExifInterface.TAG_RESOLUTION_UNIT, TYPE_INT)90 TYPE_MAPPING.put(ExifInterface.TAG_RESOLUTION_UNIT, TYPE_INT); TYPE_MAPPING.put(ExifInterface.TAG_ROWS_PER_STRIP, TYPE_INT)91 TYPE_MAPPING.put(ExifInterface.TAG_ROWS_PER_STRIP, TYPE_INT); TYPE_MAPPING.put(ExifInterface.TAG_SAMPLES_PER_PIXEL, TYPE_INT)92 TYPE_MAPPING.put(ExifInterface.TAG_SAMPLES_PER_PIXEL, TYPE_INT); TYPE_MAPPING.put(ExifInterface.TAG_SOFTWARE, TYPE_STRING)93 TYPE_MAPPING.put(ExifInterface.TAG_SOFTWARE, TYPE_STRING); TYPE_MAPPING.put(ExifInterface.TAG_STRIP_BYTE_COUNTS, TYPE_INT)94 TYPE_MAPPING.put(ExifInterface.TAG_STRIP_BYTE_COUNTS, TYPE_INT); TYPE_MAPPING.put(ExifInterface.TAG_STRIP_OFFSETS, TYPE_INT)95 TYPE_MAPPING.put(ExifInterface.TAG_STRIP_OFFSETS, TYPE_INT); TYPE_MAPPING.put(ExifInterface.TAG_TRANSFER_FUNCTION, TYPE_INT)96 TYPE_MAPPING.put(ExifInterface.TAG_TRANSFER_FUNCTION, TYPE_INT); TYPE_MAPPING.put(ExifInterface.TAG_WHITE_POINT, TYPE_DOUBLE)97 TYPE_MAPPING.put(ExifInterface.TAG_WHITE_POINT, TYPE_DOUBLE); TYPE_MAPPING.put(ExifInterface.TAG_X_RESOLUTION, TYPE_DOUBLE)98 TYPE_MAPPING.put(ExifInterface.TAG_X_RESOLUTION, TYPE_DOUBLE); TYPE_MAPPING.put(ExifInterface.TAG_Y_CB_CR_COEFFICIENTS, TYPE_DOUBLE)99 TYPE_MAPPING.put(ExifInterface.TAG_Y_CB_CR_COEFFICIENTS, TYPE_DOUBLE); TYPE_MAPPING.put(ExifInterface.TAG_Y_CB_CR_POSITIONING, TYPE_INT)100 TYPE_MAPPING.put(ExifInterface.TAG_Y_CB_CR_POSITIONING, TYPE_INT); TYPE_MAPPING.put(ExifInterface.TAG_Y_CB_CR_SUB_SAMPLING, TYPE_INT)101 TYPE_MAPPING.put(ExifInterface.TAG_Y_CB_CR_SUB_SAMPLING, TYPE_INT); TYPE_MAPPING.put(ExifInterface.TAG_Y_RESOLUTION, TYPE_DOUBLE)102 TYPE_MAPPING.put(ExifInterface.TAG_Y_RESOLUTION, TYPE_DOUBLE); TYPE_MAPPING.put(ExifInterface.TAG_APERTURE_VALUE, TYPE_DOUBLE)103 TYPE_MAPPING.put(ExifInterface.TAG_APERTURE_VALUE, TYPE_DOUBLE); TYPE_MAPPING.put(ExifInterface.TAG_BRIGHTNESS_VALUE, TYPE_DOUBLE)104 TYPE_MAPPING.put(ExifInterface.TAG_BRIGHTNESS_VALUE, TYPE_DOUBLE); TYPE_MAPPING.put(ExifInterface.TAG_CFA_PATTERN, TYPE_STRING)105 TYPE_MAPPING.put(ExifInterface.TAG_CFA_PATTERN, TYPE_STRING); TYPE_MAPPING.put(ExifInterface.TAG_COLOR_SPACE, TYPE_INT)106 TYPE_MAPPING.put(ExifInterface.TAG_COLOR_SPACE, TYPE_INT); TYPE_MAPPING.put(ExifInterface.TAG_COMPONENTS_CONFIGURATION, TYPE_STRING)107 TYPE_MAPPING.put(ExifInterface.TAG_COMPONENTS_CONFIGURATION, TYPE_STRING); TYPE_MAPPING.put(ExifInterface.TAG_COMPRESSED_BITS_PER_PIXEL, TYPE_DOUBLE)108 TYPE_MAPPING.put(ExifInterface.TAG_COMPRESSED_BITS_PER_PIXEL, TYPE_DOUBLE); TYPE_MAPPING.put(ExifInterface.TAG_CONTRAST, TYPE_INT)109 TYPE_MAPPING.put(ExifInterface.TAG_CONTRAST, TYPE_INT); TYPE_MAPPING.put(ExifInterface.TAG_CUSTOM_RENDERED, TYPE_INT)110 TYPE_MAPPING.put(ExifInterface.TAG_CUSTOM_RENDERED, TYPE_INT); TYPE_MAPPING.put(ExifInterface.TAG_DATETIME_DIGITIZED, TYPE_STRING)111 TYPE_MAPPING.put(ExifInterface.TAG_DATETIME_DIGITIZED, TYPE_STRING); TYPE_MAPPING.put(ExifInterface.TAG_DATETIME_ORIGINAL, TYPE_STRING)112 TYPE_MAPPING.put(ExifInterface.TAG_DATETIME_ORIGINAL, TYPE_STRING); TYPE_MAPPING.put(ExifInterface.TAG_DEVICE_SETTING_DESCRIPTION, TYPE_STRING)113 TYPE_MAPPING.put(ExifInterface.TAG_DEVICE_SETTING_DESCRIPTION, TYPE_STRING); TYPE_MAPPING.put(ExifInterface.TAG_DIGITAL_ZOOM_RATIO, TYPE_DOUBLE)114 TYPE_MAPPING.put(ExifInterface.TAG_DIGITAL_ZOOM_RATIO, TYPE_DOUBLE); TYPE_MAPPING.put(ExifInterface.TAG_EXIF_VERSION, TYPE_STRING)115 TYPE_MAPPING.put(ExifInterface.TAG_EXIF_VERSION, TYPE_STRING); TYPE_MAPPING.put(ExifInterface.TAG_EXPOSURE_BIAS_VALUE, TYPE_DOUBLE)116 TYPE_MAPPING.put(ExifInterface.TAG_EXPOSURE_BIAS_VALUE, TYPE_DOUBLE); TYPE_MAPPING.put(ExifInterface.TAG_EXPOSURE_INDEX, TYPE_DOUBLE)117 TYPE_MAPPING.put(ExifInterface.TAG_EXPOSURE_INDEX, TYPE_DOUBLE); TYPE_MAPPING.put(ExifInterface.TAG_EXPOSURE_MODE, TYPE_INT)118 TYPE_MAPPING.put(ExifInterface.TAG_EXPOSURE_MODE, TYPE_INT); TYPE_MAPPING.put(ExifInterface.TAG_EXPOSURE_PROGRAM, TYPE_INT)119 TYPE_MAPPING.put(ExifInterface.TAG_EXPOSURE_PROGRAM, TYPE_INT); TYPE_MAPPING.put(ExifInterface.TAG_EXPOSURE_TIME, TYPE_DOUBLE)120 TYPE_MAPPING.put(ExifInterface.TAG_EXPOSURE_TIME, TYPE_DOUBLE); TYPE_MAPPING.put(ExifInterface.TAG_F_NUMBER, TYPE_DOUBLE)121 TYPE_MAPPING.put(ExifInterface.TAG_F_NUMBER, TYPE_DOUBLE); TYPE_MAPPING.put(ExifInterface.TAG_FILE_SOURCE, TYPE_STRING)122 TYPE_MAPPING.put(ExifInterface.TAG_FILE_SOURCE, TYPE_STRING); TYPE_MAPPING.put(ExifInterface.TAG_FLASH, TYPE_INT)123 TYPE_MAPPING.put(ExifInterface.TAG_FLASH, TYPE_INT); TYPE_MAPPING.put(ExifInterface.TAG_FLASH_ENERGY, TYPE_DOUBLE)124 TYPE_MAPPING.put(ExifInterface.TAG_FLASH_ENERGY, TYPE_DOUBLE); TYPE_MAPPING.put(ExifInterface.TAG_FLASHPIX_VERSION, TYPE_STRING)125 TYPE_MAPPING.put(ExifInterface.TAG_FLASHPIX_VERSION, TYPE_STRING); TYPE_MAPPING.put(ExifInterface.TAG_FOCAL_LENGTH, TYPE_DOUBLE)126 TYPE_MAPPING.put(ExifInterface.TAG_FOCAL_LENGTH, TYPE_DOUBLE); TYPE_MAPPING.put(ExifInterface.TAG_FOCAL_LENGTH_IN_35MM_FILM, TYPE_INT)127 TYPE_MAPPING.put(ExifInterface.TAG_FOCAL_LENGTH_IN_35MM_FILM, TYPE_INT); TYPE_MAPPING.put(ExifInterface.TAG_FOCAL_PLANE_RESOLUTION_UNIT, TYPE_INT)128 TYPE_MAPPING.put(ExifInterface.TAG_FOCAL_PLANE_RESOLUTION_UNIT, TYPE_INT); TYPE_MAPPING.put(ExifInterface.TAG_FOCAL_PLANE_X_RESOLUTION, TYPE_DOUBLE)129 TYPE_MAPPING.put(ExifInterface.TAG_FOCAL_PLANE_X_RESOLUTION, TYPE_DOUBLE); TYPE_MAPPING.put(ExifInterface.TAG_FOCAL_PLANE_Y_RESOLUTION, TYPE_DOUBLE)130 TYPE_MAPPING.put(ExifInterface.TAG_FOCAL_PLANE_Y_RESOLUTION, TYPE_DOUBLE); TYPE_MAPPING.put(ExifInterface.TAG_GAIN_CONTROL, TYPE_INT)131 TYPE_MAPPING.put(ExifInterface.TAG_GAIN_CONTROL, TYPE_INT); TYPE_MAPPING.put(ExifInterface.TAG_ISO_SPEED_RATINGS, TYPE_INT)132 TYPE_MAPPING.put(ExifInterface.TAG_ISO_SPEED_RATINGS, TYPE_INT); TYPE_MAPPING.put(ExifInterface.TAG_IMAGE_UNIQUE_ID, TYPE_STRING)133 TYPE_MAPPING.put(ExifInterface.TAG_IMAGE_UNIQUE_ID, TYPE_STRING); TYPE_MAPPING.put(ExifInterface.TAG_LIGHT_SOURCE, TYPE_INT)134 TYPE_MAPPING.put(ExifInterface.TAG_LIGHT_SOURCE, TYPE_INT); TYPE_MAPPING.put(ExifInterface.TAG_MAKER_NOTE, TYPE_STRING)135 TYPE_MAPPING.put(ExifInterface.TAG_MAKER_NOTE, TYPE_STRING); TYPE_MAPPING.put(ExifInterface.TAG_MAX_APERTURE_VALUE, TYPE_DOUBLE)136 TYPE_MAPPING.put(ExifInterface.TAG_MAX_APERTURE_VALUE, TYPE_DOUBLE); TYPE_MAPPING.put(ExifInterface.TAG_METERING_MODE, TYPE_INT)137 TYPE_MAPPING.put(ExifInterface.TAG_METERING_MODE, TYPE_INT); TYPE_MAPPING.put(ExifInterface.TAG_NEW_SUBFILE_TYPE, TYPE_INT)138 TYPE_MAPPING.put(ExifInterface.TAG_NEW_SUBFILE_TYPE, TYPE_INT); TYPE_MAPPING.put(ExifInterface.TAG_OECF, TYPE_STRING)139 TYPE_MAPPING.put(ExifInterface.TAG_OECF, TYPE_STRING); TYPE_MAPPING.put(ExifInterface.TAG_PIXEL_X_DIMENSION, TYPE_INT)140 TYPE_MAPPING.put(ExifInterface.TAG_PIXEL_X_DIMENSION, TYPE_INT); TYPE_MAPPING.put(ExifInterface.TAG_PIXEL_Y_DIMENSION, TYPE_INT)141 TYPE_MAPPING.put(ExifInterface.TAG_PIXEL_Y_DIMENSION, TYPE_INT); TYPE_MAPPING.put(ExifInterface.TAG_RELATED_SOUND_FILE, TYPE_STRING)142 TYPE_MAPPING.put(ExifInterface.TAG_RELATED_SOUND_FILE, TYPE_STRING); TYPE_MAPPING.put(ExifInterface.TAG_SATURATION, TYPE_INT)143 TYPE_MAPPING.put(ExifInterface.TAG_SATURATION, TYPE_INT); TYPE_MAPPING.put(ExifInterface.TAG_SCENE_CAPTURE_TYPE, TYPE_INT)144 TYPE_MAPPING.put(ExifInterface.TAG_SCENE_CAPTURE_TYPE, TYPE_INT); TYPE_MAPPING.put(ExifInterface.TAG_SCENE_TYPE, TYPE_STRING)145 TYPE_MAPPING.put(ExifInterface.TAG_SCENE_TYPE, TYPE_STRING); TYPE_MAPPING.put(ExifInterface.TAG_SENSING_METHOD, TYPE_INT)146 TYPE_MAPPING.put(ExifInterface.TAG_SENSING_METHOD, TYPE_INT); TYPE_MAPPING.put(ExifInterface.TAG_SHARPNESS, TYPE_INT)147 TYPE_MAPPING.put(ExifInterface.TAG_SHARPNESS, TYPE_INT); TYPE_MAPPING.put(ExifInterface.TAG_SHUTTER_SPEED_VALUE, TYPE_DOUBLE)148 TYPE_MAPPING.put(ExifInterface.TAG_SHUTTER_SPEED_VALUE, TYPE_DOUBLE); TYPE_MAPPING.put(ExifInterface.TAG_SPATIAL_FREQUENCY_RESPONSE, TYPE_STRING)149 TYPE_MAPPING.put(ExifInterface.TAG_SPATIAL_FREQUENCY_RESPONSE, TYPE_STRING); TYPE_MAPPING.put(ExifInterface.TAG_SPECTRAL_SENSITIVITY, TYPE_STRING)150 TYPE_MAPPING.put(ExifInterface.TAG_SPECTRAL_SENSITIVITY, TYPE_STRING); TYPE_MAPPING.put(ExifInterface.TAG_SUBFILE_TYPE, TYPE_INT)151 TYPE_MAPPING.put(ExifInterface.TAG_SUBFILE_TYPE, TYPE_INT); TYPE_MAPPING.put(ExifInterface.TAG_SUBSEC_TIME, TYPE_STRING)152 TYPE_MAPPING.put(ExifInterface.TAG_SUBSEC_TIME, TYPE_STRING); TYPE_MAPPING.put(ExifInterface.TAG_SUBSEC_TIME_DIGITIZED, TYPE_STRING)153 TYPE_MAPPING.put(ExifInterface.TAG_SUBSEC_TIME_DIGITIZED, TYPE_STRING); TYPE_MAPPING.put(ExifInterface.TAG_SUBSEC_TIME_ORIGINAL, TYPE_STRING)154 TYPE_MAPPING.put(ExifInterface.TAG_SUBSEC_TIME_ORIGINAL, TYPE_STRING); TYPE_MAPPING.put(ExifInterface.TAG_SUBJECT_AREA, TYPE_INT)155 TYPE_MAPPING.put(ExifInterface.TAG_SUBJECT_AREA, TYPE_INT); TYPE_MAPPING.put(ExifInterface.TAG_SUBJECT_DISTANCE, TYPE_DOUBLE)156 TYPE_MAPPING.put(ExifInterface.TAG_SUBJECT_DISTANCE, TYPE_DOUBLE); TYPE_MAPPING.put(ExifInterface.TAG_SUBJECT_DISTANCE_RANGE, TYPE_INT)157 TYPE_MAPPING.put(ExifInterface.TAG_SUBJECT_DISTANCE_RANGE, TYPE_INT); TYPE_MAPPING.put(ExifInterface.TAG_SUBJECT_LOCATION, TYPE_INT)158 TYPE_MAPPING.put(ExifInterface.TAG_SUBJECT_LOCATION, TYPE_INT); TYPE_MAPPING.put(ExifInterface.TAG_USER_COMMENT, TYPE_STRING)159 TYPE_MAPPING.put(ExifInterface.TAG_USER_COMMENT, TYPE_STRING); TYPE_MAPPING.put(ExifInterface.TAG_WHITE_BALANCE, TYPE_INT)160 TYPE_MAPPING.put(ExifInterface.TAG_WHITE_BALANCE, TYPE_INT); TYPE_MAPPING.put(ExifInterface.TAG_GPS_ALTITUDE, TYPE_DOUBLE)161 TYPE_MAPPING.put(ExifInterface.TAG_GPS_ALTITUDE, TYPE_DOUBLE); TYPE_MAPPING.put(ExifInterface.TAG_GPS_ALTITUDE_REF, TYPE_INT)162 TYPE_MAPPING.put(ExifInterface.TAG_GPS_ALTITUDE_REF, TYPE_INT); TYPE_MAPPING.put(ExifInterface.TAG_GPS_AREA_INFORMATION, TYPE_STRING)163 TYPE_MAPPING.put(ExifInterface.TAG_GPS_AREA_INFORMATION, TYPE_STRING); TYPE_MAPPING.put(ExifInterface.TAG_GPS_DOP, TYPE_DOUBLE)164 TYPE_MAPPING.put(ExifInterface.TAG_GPS_DOP, TYPE_DOUBLE); TYPE_MAPPING.put(ExifInterface.TAG_GPS_DATESTAMP, TYPE_STRING)165 TYPE_MAPPING.put(ExifInterface.TAG_GPS_DATESTAMP, TYPE_STRING); TYPE_MAPPING.put(ExifInterface.TAG_GPS_DEST_BEARING, TYPE_DOUBLE)166 TYPE_MAPPING.put(ExifInterface.TAG_GPS_DEST_BEARING, TYPE_DOUBLE); TYPE_MAPPING.put(ExifInterface.TAG_GPS_DEST_BEARING_REF, TYPE_STRING)167 TYPE_MAPPING.put(ExifInterface.TAG_GPS_DEST_BEARING_REF, TYPE_STRING); TYPE_MAPPING.put(ExifInterface.TAG_GPS_DEST_DISTANCE, TYPE_DOUBLE)168 TYPE_MAPPING.put(ExifInterface.TAG_GPS_DEST_DISTANCE, TYPE_DOUBLE); TYPE_MAPPING.put(ExifInterface.TAG_GPS_DEST_DISTANCE_REF, TYPE_STRING)169 TYPE_MAPPING.put(ExifInterface.TAG_GPS_DEST_DISTANCE_REF, TYPE_STRING); TYPE_MAPPING.put(ExifInterface.TAG_GPS_DEST_LATITUDE, TYPE_DOUBLE)170 TYPE_MAPPING.put(ExifInterface.TAG_GPS_DEST_LATITUDE, TYPE_DOUBLE); TYPE_MAPPING.put(ExifInterface.TAG_GPS_DEST_LATITUDE_REF, TYPE_STRING)171 TYPE_MAPPING.put(ExifInterface.TAG_GPS_DEST_LATITUDE_REF, TYPE_STRING); TYPE_MAPPING.put(ExifInterface.TAG_GPS_DEST_LONGITUDE, TYPE_DOUBLE)172 TYPE_MAPPING.put(ExifInterface.TAG_GPS_DEST_LONGITUDE, TYPE_DOUBLE); TYPE_MAPPING.put(ExifInterface.TAG_GPS_DEST_LONGITUDE_REF, TYPE_STRING)173 TYPE_MAPPING.put(ExifInterface.TAG_GPS_DEST_LONGITUDE_REF, TYPE_STRING); TYPE_MAPPING.put(ExifInterface.TAG_GPS_DIFFERENTIAL, TYPE_INT)174 TYPE_MAPPING.put(ExifInterface.TAG_GPS_DIFFERENTIAL, TYPE_INT); TYPE_MAPPING.put(ExifInterface.TAG_GPS_IMG_DIRECTION, TYPE_DOUBLE)175 TYPE_MAPPING.put(ExifInterface.TAG_GPS_IMG_DIRECTION, TYPE_DOUBLE); TYPE_MAPPING.put(ExifInterface.TAG_GPS_IMG_DIRECTION_REF, TYPE_STRING)176 TYPE_MAPPING.put(ExifInterface.TAG_GPS_IMG_DIRECTION_REF, TYPE_STRING); TYPE_MAPPING.put(ExifInterface.TAG_GPS_LATITUDE, TYPE_STRING)177 TYPE_MAPPING.put(ExifInterface.TAG_GPS_LATITUDE, TYPE_STRING); TYPE_MAPPING.put(ExifInterface.TAG_GPS_LATITUDE_REF, TYPE_STRING)178 TYPE_MAPPING.put(ExifInterface.TAG_GPS_LATITUDE_REF, TYPE_STRING); TYPE_MAPPING.put(ExifInterface.TAG_GPS_LONGITUDE, TYPE_STRING)179 TYPE_MAPPING.put(ExifInterface.TAG_GPS_LONGITUDE, TYPE_STRING); TYPE_MAPPING.put(ExifInterface.TAG_GPS_LONGITUDE_REF, TYPE_STRING)180 TYPE_MAPPING.put(ExifInterface.TAG_GPS_LONGITUDE_REF, TYPE_STRING); TYPE_MAPPING.put(ExifInterface.TAG_GPS_MAP_DATUM, TYPE_STRING)181 TYPE_MAPPING.put(ExifInterface.TAG_GPS_MAP_DATUM, TYPE_STRING); TYPE_MAPPING.put(ExifInterface.TAG_GPS_MEASURE_MODE, TYPE_STRING)182 TYPE_MAPPING.put(ExifInterface.TAG_GPS_MEASURE_MODE, TYPE_STRING); TYPE_MAPPING.put(ExifInterface.TAG_GPS_PROCESSING_METHOD, TYPE_STRING)183 TYPE_MAPPING.put(ExifInterface.TAG_GPS_PROCESSING_METHOD, TYPE_STRING); TYPE_MAPPING.put(ExifInterface.TAG_GPS_SATELLITES, TYPE_STRING)184 TYPE_MAPPING.put(ExifInterface.TAG_GPS_SATELLITES, TYPE_STRING); TYPE_MAPPING.put(ExifInterface.TAG_GPS_SPEED, TYPE_DOUBLE)185 TYPE_MAPPING.put(ExifInterface.TAG_GPS_SPEED, TYPE_DOUBLE); TYPE_MAPPING.put(ExifInterface.TAG_GPS_SPEED_REF, TYPE_STRING)186 TYPE_MAPPING.put(ExifInterface.TAG_GPS_SPEED_REF, TYPE_STRING); TYPE_MAPPING.put(ExifInterface.TAG_GPS_STATUS, TYPE_STRING)187 TYPE_MAPPING.put(ExifInterface.TAG_GPS_STATUS, TYPE_STRING); TYPE_MAPPING.put(ExifInterface.TAG_GPS_TIMESTAMP, TYPE_STRING)188 TYPE_MAPPING.put(ExifInterface.TAG_GPS_TIMESTAMP, TYPE_STRING); TYPE_MAPPING.put(ExifInterface.TAG_GPS_TRACK, TYPE_DOUBLE)189 TYPE_MAPPING.put(ExifInterface.TAG_GPS_TRACK, TYPE_DOUBLE); TYPE_MAPPING.put(ExifInterface.TAG_GPS_TRACK_REF, TYPE_STRING)190 TYPE_MAPPING.put(ExifInterface.TAG_GPS_TRACK_REF, TYPE_STRING); TYPE_MAPPING.put(ExifInterface.TAG_GPS_VERSION_ID, TYPE_STRING)191 TYPE_MAPPING.put(ExifInterface.TAG_GPS_VERSION_ID, TYPE_STRING); TYPE_MAPPING.put(ExifInterface.TAG_INTEROPERABILITY_INDEX, TYPE_STRING)192 TYPE_MAPPING.put(ExifInterface.TAG_INTEROPERABILITY_INDEX, TYPE_STRING); TYPE_MAPPING.put(ExifInterface.TAG_THUMBNAIL_IMAGE_LENGTH, TYPE_INT)193 TYPE_MAPPING.put(ExifInterface.TAG_THUMBNAIL_IMAGE_LENGTH, TYPE_INT); TYPE_MAPPING.put(ExifInterface.TAG_THUMBNAIL_IMAGE_WIDTH, TYPE_INT)194 TYPE_MAPPING.put(ExifInterface.TAG_THUMBNAIL_IMAGE_WIDTH, TYPE_INT); TYPE_MAPPING.put(ExifInterface.TAG_DNG_VERSION, TYPE_INT)195 TYPE_MAPPING.put(ExifInterface.TAG_DNG_VERSION, TYPE_INT); TYPE_MAPPING.put(ExifInterface.TAG_DEFAULT_CROP_SIZE, TYPE_INT)196 TYPE_MAPPING.put(ExifInterface.TAG_DEFAULT_CROP_SIZE, TYPE_INT); 197 //I don't know how to represent this. Type is unknown 198 //TYPE_MAPPING.put(ExifInterface.TAG_ORF_THUMBNAIL_IMAGE, TYPE_STRING); TYPE_MAPPING.put(ExifInterface.TAG_ORF_PREVIEW_IMAGE_START, TYPE_INT)199 TYPE_MAPPING.put(ExifInterface.TAG_ORF_PREVIEW_IMAGE_START, TYPE_INT); TYPE_MAPPING.put(ExifInterface.TAG_ORF_PREVIEW_IMAGE_LENGTH, TYPE_INT)200 TYPE_MAPPING.put(ExifInterface.TAG_ORF_PREVIEW_IMAGE_LENGTH, TYPE_INT); TYPE_MAPPING.put(ExifInterface.TAG_ORF_ASPECT_FRAME, TYPE_INT)201 TYPE_MAPPING.put(ExifInterface.TAG_ORF_ASPECT_FRAME, TYPE_INT); TYPE_MAPPING.put(ExifInterface.TAG_RW2_SENSOR_BOTTOM_BORDER, TYPE_INT)202 TYPE_MAPPING.put(ExifInterface.TAG_RW2_SENSOR_BOTTOM_BORDER, TYPE_INT); TYPE_MAPPING.put(ExifInterface.TAG_RW2_SENSOR_LEFT_BORDER, TYPE_INT)203 TYPE_MAPPING.put(ExifInterface.TAG_RW2_SENSOR_LEFT_BORDER, TYPE_INT); TYPE_MAPPING.put(ExifInterface.TAG_RW2_SENSOR_RIGHT_BORDER, TYPE_INT)204 TYPE_MAPPING.put(ExifInterface.TAG_RW2_SENSOR_RIGHT_BORDER, TYPE_INT); TYPE_MAPPING.put(ExifInterface.TAG_RW2_SENSOR_TOP_BORDER, TYPE_INT)205 TYPE_MAPPING.put(ExifInterface.TAG_RW2_SENSOR_TOP_BORDER, TYPE_INT); TYPE_MAPPING.put(ExifInterface.TAG_RW2_ISO, TYPE_INT)206 TYPE_MAPPING.put(ExifInterface.TAG_RW2_ISO, TYPE_INT); 207 } 208 private static final String JPG_MIME_TYPE = "image/jpg"; 209 private static final String JPEG_MIME_TYPE = "image/jpeg"; 210 211 212 /** 213 * Returns true if caller can generally expect to get metadata results 214 * for the supplied mimetype. 215 */ isSupportedMimeType(String mimeType)216 public static boolean isSupportedMimeType(String mimeType) { 217 return JPG_MIME_TYPE.equals(mimeType) || JPEG_MIME_TYPE.equals(mimeType); 218 } 219 220 /** 221 * Generic metadata retrieval method that can retrieve any available metadata from a given doc 222 * Currently only functions for exifdata 223 * 224 * @param metadata the bundle to which we add any relevant metadata 225 * @param stream InputStream containing a file 226 * @param mimeType type of the given file 227 * @param tags a variable amount of keys to differentiate which tags the user wants 228 * if null, returns a default set of data. See {@link DEFAULT_EXIF_TAGS}. 229 * @throws IOException when the file doesn't exist 230 */ getMetadata(Bundle metadata, InputStream stream, String mimeType, @Nullable String[] tags)231 public static void getMetadata(Bundle metadata, InputStream stream, String mimeType, 232 @Nullable String[] tags) throws IOException { 233 List<String> metadataTypes = new ArrayList<>(); 234 if (isSupportedMimeType(mimeType)) { 235 Bundle exifData = getExifData(stream, tags); 236 if (exifData.size() > 0) { 237 metadata.putBundle(DocumentsContract.METADATA_EXIF, exifData); 238 metadataTypes.add(DocumentsContract.METADATA_EXIF); 239 } 240 } 241 metadata.putStringArray(DocumentsContract.METADATA_TYPES, 242 metadataTypes.toArray(new String[metadataTypes.size()])); 243 // TODO: Add support for PDF and Video metadata 244 // TODO: Broaden image support to all images 245 } 246 247 /** 248 * Helper method that is called if getMetadata is called for an image mimeType. 249 * 250 * @param stream the input stream from which to extra data. 251 * @param tags a list of ExifInterface tags that are used to retrieve data. 252 * if null, returns a default set of data. See {@link DEFAULT_EXIF_TAGS}. 253 */ getExifData(InputStream stream, @Nullable String[] tags)254 private static Bundle getExifData(InputStream stream, @Nullable String[] tags) 255 throws IOException { 256 if (tags == null) { 257 tags = DEFAULT_EXIF_TAGS; 258 } 259 260 ExifInterface exifInterface = new ExifInterface(stream); 261 Bundle exif = new Bundle(); 262 for (String tag : tags) { 263 if (TYPE_MAPPING.get(tag).equals(TYPE_INT)) { 264 int data = exifInterface.getAttributeInt(tag, Integer.MIN_VALUE); 265 if (data != Integer.MIN_VALUE) { 266 exif.putInt(tag, data); 267 } 268 } else if (TYPE_MAPPING.get(tag).equals(TYPE_DOUBLE)) { 269 double data = exifInterface.getAttributeDouble(tag, Double.MIN_VALUE); 270 if (data != Double.MIN_VALUE) { 271 exif.putDouble(tag, data); 272 } 273 } else if (TYPE_MAPPING.get(tag).equals(TYPE_STRING)) { 274 String data = exifInterface.getAttribute(tag); 275 if (data != null) { 276 exif.putString(tag, data); 277 } 278 } 279 } 280 return exif; 281 } 282 } 283