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