[Android] Avoid decoding too large bitmaps over http
Nicolas Pomepuy
git at videolan.org
Fri Mar 13 12:41:49 CET 2020
vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Fri Mar 13 09:22:04 2020 +0100| [72134aad5cdedbfa7109aa7f7af190a7a2e8633c] | committer: Nicolas Pomepuy
Avoid decoding too large bitmaps over http
Fixes #1206
> https://code.videolan.org/videolan/vlc-android/commit/72134aad5cdedbfa7109aa7f7af190a7a2e8633c
---
.../main/java/org/videolan/tools/HttpImageLoader.kt | 21 ++++++++++++++++++---
1 file changed, 18 insertions(+), 3 deletions(-)
diff --git a/application/tools/src/main/java/org/videolan/tools/HttpImageLoader.kt b/application/tools/src/main/java/org/videolan/tools/HttpImageLoader.kt
index 143ba1bb8..8ea559c09 100644
--- a/application/tools/src/main/java/org/videolan/tools/HttpImageLoader.kt
+++ b/application/tools/src/main/java/org/videolan/tools/HttpImageLoader.kt
@@ -24,16 +24,17 @@
package org.videolan.tools
+
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.util.Log
-
-
import java.io.BufferedInputStream
import java.io.IOException
import java.io.InputStream
import java.net.HttpURLConnection
import java.net.URL
+import kotlin.math.floor
+import kotlin.math.max
object HttpImageLoader {
@@ -46,7 +47,21 @@ object HttpImageLoader {
val url = URL(imageUrl)
urlConnection = url.openConnection() as HttpURLConnection
inputStream = BufferedInputStream(urlConnection.inputStream)
- icon = BitmapFactory.decodeStream(inputStream)
+
+ val options = BitmapFactory.Options()
+ options.inJustDecodeBounds = true
+ BitmapFactory.decodeStream(inputStream, null, options)
+ options.inJustDecodeBounds = false
+
+ //limit image to 150dp for the larger size
+ val ratio: Float = max(options.outHeight, options.outWidth).toFloat() / 150.dp.toFloat()
+ if (ratio > 1) {
+ options.inSampleSize = floor(ratio).toInt()
+ }
+
+ urlConnection = url.openConnection() as HttpURLConnection
+ inputStream = BufferedInputStream(urlConnection.inputStream)
+ icon = BitmapFactory.decodeStream(inputStream, null, options)
BitmapCache.addBitmapToMemCache(imageUrl, icon)
} catch (ignored: IOException) {
Log.e("", ignored.message, ignored)
More information about the Android
mailing list