From 6a6e6019c02fe743b3d4edd4de16e2789f0aac32 Mon Sep 17 00:00:00 2001 From: Nivetha Singara Vadivelu Date: Tue, 15 Aug 2017 11:36:44 -0700 Subject: [PATCH] Fetching video length Reviewed By: furdei Differential Revision: D5596545 fbshipit-source-id: ae29bc27579f2d06b1281e677c1aa820d50d9ee2 --- CameraRoll.js | 4 +--- CameraRollManager.java | 46 ++++++++++++++++++++++++++++-------------- 2 files changed, 32 insertions(+), 18 deletions(-) diff --git a/CameraRoll.js b/CameraRoll.js index 504878735..e4a22a460 100644 --- a/CameraRoll.js +++ b/CameraRoll.js @@ -88,9 +88,7 @@ const getPhotosReturnChecker = createStrictShapeTypeChecker({ height: PropTypes.number.isRequired, width: PropTypes.number.isRequired, isStored: PropTypes.bool, - // TODO (nivethavadivelu) Need to add changes to Android before - // setting it as required - playableDuration: PropTypes.number, + playableDuration: PropTypes.number.isRequired, }).isRequired, timestamp: PropTypes.number.isRequired, location: createStrictShapeTypeChecker({ diff --git a/CameraRollManager.java b/CameraRollManager.java index 1f5c7a600..6fc28ec86 100644 --- a/CameraRollManager.java +++ b/CameraRollManager.java @@ -411,30 +411,46 @@ public class CameraRollManager extends ReactContextBaseJavaModule { width = photos.getInt(widthIndex); height = photos.getInt(heightIndex); } - if (width <= 0 || height <= 0) { + + if (assetType != null + && assetType.equals("Videos") + && android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD_MR1) { try { AssetFileDescriptor photoDescriptor = resolver.openAssetFileDescriptor(photoUri, "r"); - if (assetType != null - && assetType.equals("Videos") - && android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD_MR1) { - MediaMetadataRetriever retriever = new MediaMetadataRetriever(); - retriever.setDataSource(photoDescriptor.getFileDescriptor()); + MediaMetadataRetriever retriever = new MediaMetadataRetriever(); + retriever.setDataSource(photoDescriptor.getFileDescriptor()); + + if (width <= 0 || height <= 0) { width = Integer.parseInt( retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH)); height = Integer.parseInt( retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT)); - retriever.release(); - } else { - BitmapFactory.Options options = new BitmapFactory.Options(); - // Set inJustDecodeBounds to true so we don't actually load the Bitmap, but only get its - // dimensions instead. - options.inJustDecodeBounds = true; - BitmapFactory.decodeFileDescriptor(photoDescriptor.getFileDescriptor(), null, options); - width = options.outWidth; - height = options.outHeight; } + int timeInMillisec = + Integer.parseInt( + retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION)); + int playableDuration = timeInMillisec / 1000; + image.putInt("playableDuration", playableDuration); + retriever.release(); + photoDescriptor.close(); + } catch (IOException e) { + FLog.e(ReactConstants.TAG, "Could not get video metadata for " + photoUri.toString(), e); + return false; + } + } + + if (width <= 0 || height <= 0) { + try { + AssetFileDescriptor photoDescriptor = resolver.openAssetFileDescriptor(photoUri, "r"); + BitmapFactory.Options options = new BitmapFactory.Options(); + // Set inJustDecodeBounds to true so we don't actually load the Bitmap, but only get its + // dimensions instead. + options.inJustDecodeBounds = true; + BitmapFactory.decodeFileDescriptor(photoDescriptor.getFileDescriptor(), null, options); + width = options.outWidth; + height = options.outHeight; photoDescriptor.close(); } catch (IOException e) { FLog.e(ReactConstants.TAG, "Could not get width/height for " + photoUri.toString(), e);