[Android] Prevent a crash when thumbnail size is too large for widgets

Nicolas Pomepuy git at videolan.org
Mon Jan 27 16:52:16 CET 2020


vlc-android | branch: 3.2.x | Nicolas Pomepuy <nicolas at videolabs.io> | Mon Jan 27 16:28:43 2020 +0100| [70567f48e1f10914ecef7ceeca05554330c00f50] | committer: Geoffrey Métais

Prevent a crash when thumbnail size is too large for widgets

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

 .../src/org/videolan/vlc/widget/VLCAppWidgetProvider.kt | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/widget/VLCAppWidgetProvider.kt b/vlc-android/src/org/videolan/vlc/widget/VLCAppWidgetProvider.kt
index 43bab8233..f8d1d8b82 100644
--- a/vlc-android/src/org/videolan/vlc/widget/VLCAppWidgetProvider.kt
+++ b/vlc-android/src/org/videolan/vlc/widget/VLCAppWidgetProvider.kt
@@ -27,10 +27,13 @@ import android.appwidget.AppWidgetProvider
 import android.content.ComponentName
 import android.content.Context
 import android.content.Intent
+import android.graphics.Bitmap
 import android.net.Uri
 import android.os.Build
 import android.text.TextUtils
+import android.util.DisplayMetrics
 import android.view.View
+import android.view.WindowManager
 import android.widget.RemoteViews
 import kotlinx.coroutines.ExperimentalCoroutinesApi
 import kotlinx.coroutines.ObsoleteCoroutinesApi
@@ -112,10 +115,12 @@ abstract class VLCAppWidgetProvider : AppWidgetProvider() {
                 if (!TextUtils.isEmpty(artworkMrl)) {
                     runIO(Runnable {
                         val cover = AudioUtil.readCoverBitmap(Uri.decode(artworkMrl), 320)
+                        val wm = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
+                        val dm = DisplayMetrics().also { wm.defaultDisplay.getMetrics(it) }
                         runOnMainThread(Runnable {
-                            if (cover != null)
-                                views.setImageViewBitmap(R.id.cover, cover)
-                            else
+                            if (cover != null) {
+                                if (cover.byteSize() < dm.widthPixels * dm.heightPixels * 6) views.setImageViewBitmap(R.id.cover, cover)
+                            } else
                                 views.setImageViewResource(R.id.cover, R.drawable.icon)
                             views.setProgressBar(R.id.timeline, 100, 0, false)
                             applyUpdate(context, views, partial)
@@ -167,3 +172,9 @@ abstract class VLCAppWidgetProvider : AppWidgetProvider() {
         val ACTION_WIDGET_DISABLED = ACTION_WIDGET_PREFIX + "DISABLED"
     }
 }
+fun Bitmap.byteSize(): Int {
+    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
+        return allocationByteCount
+    }
+    return rowBytes * height
+}



More information about the Android mailing list