fun createImageThumbnail(filePath: String, kind: Int): Bitmap? { val wantMini = kind == Images.Thumbnails.MINI_KIND val targetSize: Int = If (wantMini) TARGET_SIZE_MINI_THUMBNAIL else TARGET_SIZE_MICRO_THUMBNAIL val maxPixels: Int = if (wantMini) MAX_NUM_PIXELS_THUMBNAIL else MAX_NUM_PIXELS_MICRO_THUMBNAIL val sizedThumbnailBitmap = SizedThumbnailBitmap() var bitmap: Bitmap? = null val fileType: MediaFileType = MediaFile.getFileType(filePath) if (fileType != null && fileType.fileType === MediaFile.FILE_TYPE_JPEG) { createThumbnailFromEXIF(filePath, targetSize, maxPixels, sizedThumbnailBitmap) bitmap = sizedThumbnailBitmap.mBitmap } if (bitmap == null) { var stream: FileInputStream? = null try { stream = FileInputStream(filePath) val fd: FileDescriptor = stream.getFD() val options = BitmapFactory.Options() options.inSampleSize = 1 options.inJustDecodeBounds = true BitmapFactory.decodeFileDescriptor(fd, null, options) if (options.mCancel || options.outWidth == -1 || options.outHeight == -1) { return null } options.inSampleSize = computeSampleSize(options, targetSize, maxPixels) options.inJustDecodeBounds = false options.inDither = false options.inPreferredConfig = Bitmap.Config.ARGB_8888 bitmap = BitmapFactory.decodeFileDescriptor(fd, null, options) } catch (ex: IOException) { Log.e(TAG, "", ex) } catch (oom: OutOfMemoryError) { Log.e(TAG, "Unable to decode file $filePath. OutOfMemoryError.", oom) } finally { try { if (stream != null) { stream.close() } } catch (ex: IOException) { Log.e(TAG, "", ex) } } } if (kind == Images.Thumbnails.MICRO_KIND) { bitmap = extractThumbnail( bitmap, TARGET_SIZE_MICRO_THUMBNAIL, TARGET_SIZE_MICRO_THUMBNAIL, OPTIONS_RECYCLE_INPUT ) } return bitmap }
This method first examines if the thumbnail embedded in EXIF is bigger than our target size . If not , then it 'll create a thumbnail from original image . Due to efficiency consideration , we want to let MediaThumbRequest avoid calling this method twice for both kinds , so it only requests for MICRO_KIND and set saveImage to true . This method always returns a `` square thumbnail '' for MICRO_KIND thumbnail .