-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: video thumbnail generation crash
- Loading branch information
Showing
5 changed files
with
95 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
diff --git a/node_modules/expo-video-thumbnails/android/src/main/java/expo/modules/videothumbnails/VideoThumbnailsModule.kt b/node_modules/expo-video-thumbnails/android/src/main/java/expo/modules/videothumbnails/VideoThumbnailsModule.kt | ||
index 4da98be..71400ab 100644 | ||
--- a/node_modules/expo-video-thumbnails/android/src/main/java/expo/modules/videothumbnails/VideoThumbnailsModule.kt | ||
+++ b/node_modules/expo-video-thumbnails/android/src/main/java/expo/modules/videothumbnails/VideoThumbnailsModule.kt | ||
@@ -40,22 +40,27 @@ class VideoThumbnailsModule : Module() { | ||
val thumbnail = GetThumbnail(sourceFilename, options, context).execute() | ||
?: throw GenerateThumbnailException() | ||
|
||
- try { | ||
- val path = FileUtilities.generateOutputPath(context.cacheDir, "VideoThumbnails", "jpg") | ||
- FileOutputStream(path).use { outputStream -> | ||
- thumbnail.compress(Bitmap.CompressFormat.JPEG, (options.quality * 100).toInt(), outputStream) | ||
- } | ||
- promise.resolve( | ||
- VideoThumbnailResult( | ||
- uri = Uri.fromFile(File(path)).toString(), | ||
- width = thumbnail.width, | ||
- height = thumbnail.height | ||
+ if(thumbnail == null){ | ||
+ promise.reject(ERROR_TAG, "Thumbnail generation failed", GenerateThumbnailException()) | ||
+ } | ||
+ else{ | ||
+ try { | ||
+ val path = FileUtilities.generateOutputPath(context.cacheDir, "VideoThumbnails", "jpg") | ||
+ FileOutputStream(path).use { outputStream -> | ||
+ thumbnail.compress(Bitmap.CompressFormat.JPEG, (options.quality * 100).toInt(), outputStream) | ||
+ } | ||
+ promise.resolve( | ||
+ VideoThumbnailResult( | ||
+ uri = Uri.fromFile(File(path)).toString(), | ||
+ width = thumbnail.width, | ||
+ height = thumbnail.height | ||
+ ) | ||
) | ||
- ) | ||
- } catch (ex: IOException) { | ||
- promise.reject(ERROR_TAG, ex.message, ex) | ||
- } catch (ex: RuntimeException) { | ||
- promise.reject(ERROR_TAG, ex.message, ex) | ||
+ } catch (ex: IOException) { | ||
+ promise.reject(ERROR_TAG, ex.message, ex) | ||
+ } catch (ex: RuntimeException) { | ||
+ promise.reject(ERROR_TAG, ex.message, ex) | ||
+ } | ||
} | ||
} | ||
} | ||
@@ -71,21 +76,28 @@ class VideoThumbnailsModule : Module() { | ||
|
||
private class GetThumbnail(private val sourceFilename: String, private val videoOptions: VideoThumbnailOptions, private val context: Context) { | ||
fun execute(): Bitmap? { | ||
- val retriever = MediaMetadataRetriever() | ||
+ try{ | ||
+ val retriever = MediaMetadataRetriever() | ||
|
||
- if (URLUtil.isFileUrl(sourceFilename)) { | ||
- retriever.setDataSource(Uri.decode(sourceFilename).replace("file://", "")) | ||
- } else if (URLUtil.isContentUrl(sourceFilename)) { | ||
- val fileUri = Uri.parse(sourceFilename) | ||
- val fileDescriptor = context.contentResolver.openFileDescriptor(fileUri, "r")!!.fileDescriptor | ||
- FileInputStream(fileDescriptor).use { inputStream -> | ||
- retriever.setDataSource(inputStream.fd) | ||
+ if (URLUtil.isFileUrl(sourceFilename)) { | ||
+ retriever.setDataSource(Uri.decode(sourceFilename).replace("file://", "")) | ||
+ } else if (URLUtil.isContentUrl(sourceFilename)) { | ||
+ val fileUri = Uri.parse(sourceFilename) | ||
+ val fileDescriptor = context.contentResolver.openFileDescriptor(fileUri, "r")!!.fileDescriptor | ||
+ FileInputStream(fileDescriptor).use { inputStream -> | ||
+ retriever.setDataSource(inputStream.fd) | ||
+ } | ||
+ } else { | ||
+ retriever.setDataSource(sourceFilename, videoOptions.headers) | ||
} | ||
- } else { | ||
- retriever.setDataSource(sourceFilename, videoOptions.headers) | ||
+ | ||
+ return retriever.getFrameAtTime(videoOptions.time.toLong() * 1000, MediaMetadataRetriever.OPTION_CLOSEST_SYNC) | ||
} | ||
+ catch(e: RuntimeException){ | ||
+ Log.e(ERROR_TAG, "setDataSource failed") | ||
|
||
- return retriever.getFrameAtTime(videoOptions.time.toLong() * 1000, MediaMetadataRetriever.OPTION_CLOSEST_SYNC) | ||
+ return null | ||
+ } | ||
} | ||
} | ||
|