[Android] Fix video screenshot crash

Nicolas Pomepuy git at videolan.org
Mon May 13 09:09:10 UTC 2024


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Mon May  6 08:13:57 2024 +0200| [0e73622ccec1e5622eeeee2a4bf3309c39daf218] | committer: Nicolas Pomepuy

Fix video screenshot crash

> https://code.videolan.org/videolan/vlc-android/commit/0e73622ccec1e5622eeeee2a4bf3309c39daf218
---

 .../videolan/vlc/gui/video/VideoPlayerActivity.kt  | 49 +++++++++++++---------
 1 file changed, 30 insertions(+), 19 deletions(-)

diff --git a/application/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.kt b/application/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.kt
index 60c52c5367..4b490d7cba 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.kt
@@ -970,25 +970,36 @@ open class VideoPlayerActivity : AppCompatActivity(), PlaybackService.Callback,
                 videoLayout?.findViewById<FrameLayout>(R.id.player_surface_frame)?.let {
                     val surfaceView = it.findViewById<SurfaceView>(R.id.surface_video)
                     surfaceView?.let { surface ->
-                        val width = service?.currentVideoTrack?.getWidth() ?: surface.width
-                        val height = service?.currentVideoTrack?.getHeight() ?: surface.height
-                        val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
-                        val simpleDateFormat = SimpleDateFormat("yyyyMMdd_HHmmss")
-                        AndroidDevices.MediaFolders.EXTERNAL_PUBLIC_SCREENSHOTS_URI_DIRECTORY.toFile().mkdirs()
-                        val dst = File(AndroidDevices.MediaFolders.EXTERNAL_PUBLIC_SCREENSHOTS_URI_DIRECTORY.path + "/vlc_${simpleDateFormat.format(Date())}.jpg")
-
-                        PixelCopy.request(surface, bitmap, { copyResult ->
-                            if (copyResult != 0) {
-                                UiTools.snacker(this at VideoPlayerActivity, R.string.screenshot_error)
-                                return at request
-                            }
-                            val coords = IntArray(2)
-                            surfaceView.getLocationOnScreen(coords)
-                            if (BitmapUtil.saveOnDisk(bitmap, dst.absolutePath))
-                                screenshotDelegate.takeScreenshot(dst, bitmap, coords, surface.width, surface.height)
-                            else
-                                UiTools.snacker(this at VideoPlayerActivity, R.string.screenshot_error)
-                        }, Handler(Looper.getMainLooper()))
+                        var width = 0
+                        var height = 0
+                        service?.currentVideoTrack?.let {
+                            width = it.getWidth()
+                            height = it.getHeight()
+                        }
+                        if (width == 0) width = surface.width
+                        if (height == 0) height = surface.width
+                        try {
+                            val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
+                            val simpleDateFormat = SimpleDateFormat("yyyyMMdd_HHmmss")
+                            AndroidDevices.MediaFolders.EXTERNAL_PUBLIC_SCREENSHOTS_URI_DIRECTORY.toFile().mkdirs()
+                            val dst = File(AndroidDevices.MediaFolders.EXTERNAL_PUBLIC_SCREENSHOTS_URI_DIRECTORY.path + "/vlc_${simpleDateFormat.format(Date())}.jpg")
+
+                            PixelCopy.request(surface, bitmap, { copyResult ->
+                                if (copyResult != 0) {
+                                    UiTools.snacker(this at VideoPlayerActivity, R.string.screenshot_error)
+                                    return at request
+                                }
+                                val coords = IntArray(2)
+                                surfaceView.getLocationOnScreen(coords)
+                                if (BitmapUtil.saveOnDisk(bitmap, dst.absolutePath))
+                                    screenshotDelegate.takeScreenshot(dst, bitmap, coords, surface.width, surface.height)
+                                else
+                                    UiTools.snacker(this at VideoPlayerActivity, R.string.screenshot_error)
+                            }, Handler(Looper.getMainLooper()))
+                        } catch (e: Exception) {
+                            Log.e(TAG, e.message, e)
+                            UiTools.snacker(this at VideoPlayerActivity, R.string.screenshot_error)
+                        }
                     }
                 }
 



More information about the Android mailing list