[Android] Screenshots: fix the save issue and check write permission beforehand

Nicolas Pomepuy git at videolan.org
Wed Jul 27 13:34:21 UTC 2022


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Wed Jul 27 13:58:05 2022 +0200| [b559dc07f3ecc11d11adfc05cc7d9833e9202fb5] | committer: Duncan McNamara

Screenshots: fix the save issue and check write permission beforehand

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

 .../src/org/videolan/vlc/gui/helpers/BitmapUtil.kt          | 13 +++++++++----
 .../src/org/videolan/vlc/gui/video/VideoPlayerActivity.kt   | 10 ++++++++--
 2 files changed, 17 insertions(+), 6 deletions(-)

diff --git a/application/vlc-android/src/org/videolan/vlc/gui/helpers/BitmapUtil.kt b/application/vlc-android/src/org/videolan/vlc/gui/helpers/BitmapUtil.kt
index 159088c0b..5fd343527 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/helpers/BitmapUtil.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/helpers/BitmapUtil.kt
@@ -285,20 +285,25 @@ object BitmapUtil {
         else -> Rect(0, 0, width, height)
     }
 
-    fun saveOnDisk(bitmap: Bitmap, destPath: String) {
+    fun saveOnDisk(bitmap: Bitmap, destPath: String):Boolean {
         val destFile = File(destPath)
-        when {
-            destFile.canWrite() -> {
+        return when {
+            destFile.parentFile?.canWrite() == true -> {
                 try {
                     ByteArrayOutputStream().use { stream ->
                         bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream)
                         FileOutputStream(destFile).use { it.write(stream.toByteArray()) }
                     }
+                    true
                 } catch (e: IOException) {
                     Log.e(TAG, "Could not save image to disk", e)
+                    false
                 }
             }
-            else -> Log.e(TAG, "File path not writable: $destFile")
+            else -> {
+                Log.e(TAG, "File path not writable: $destFile")
+                false
+            }
         }
     }
 
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 f0c551654..a4635ed7f 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
@@ -904,6 +904,10 @@ open class VideoPlayerActivity : AppCompatActivity(), PlaybackService.Callback,
      *
      */
     fun takeScreenshot() {
+        if (AndroidUtil.isOOrLater && !Permissions.canWriteStorage(this)) {
+            Permissions.askWriteStoragePermission(this, false){}
+            return
+        }
         lifecycleScope.launch {
             withContext(Dispatchers.IO) {
                 videoLayout?.findViewById<FrameLayout>(R.id.player_surface_frame)?.let {
@@ -923,8 +927,10 @@ open class VideoPlayerActivity : AppCompatActivity(), PlaybackService.Callback,
                             }
                             val coords = IntArray(2)
                             surfaceView.getLocationOnScreen(coords)
-                            screenshotDelegate.takeScreenshot(dst, bitmap, coords, surface.width, surface.height)
-                            BitmapUtil.saveOnDisk(bitmap, dst.absolutePath)
+                            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()))
                     }
                 }



More information about the Android mailing list