From 4c70706d49a8450bc84acba64738661ddc5a09d7 Mon Sep 17 00:00:00 2001 From: tibbi Date: Wed, 14 Mar 2018 23:01:45 +0100 Subject: [PATCH] try loading images in half resolution if they fail because of out of memory --- .../gallery/fragments/PhotoFragment.kt | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/fragments/PhotoFragment.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/fragments/PhotoFragment.kt index 6e65b49b9..a6df99c97 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/fragments/PhotoFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/fragments/PhotoFragment.kt @@ -10,6 +10,7 @@ import android.media.ExifInterface.* import android.net.Uri import android.os.AsyncTask import android.os.Bundle +import android.os.Handler import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -44,6 +45,7 @@ class PhotoFragment : ViewPagerFragment() { private var isFragmentVisible = false private var isFullscreen = false private var wasInit = false + private var useHalfResolution = false private var imageOrientation = -1 private var gifDrawable: GifDrawable? = null @@ -231,6 +233,11 @@ class PhotoFragment : ViewPagerFragment() { if (degrees == 0f) { var targetWidth = if (ViewPagerActivity.screenWidth == 0) Target.SIZE_ORIGINAL else ViewPagerActivity.screenWidth var targetHeight = if (ViewPagerActivity.screenHeight == 0) Target.SIZE_ORIGINAL else ViewPagerActivity.screenHeight + if (useHalfResolution) { + targetWidth /= 2 + targetHeight /= 2 + } + if (imageOrientation == ORIENTATION_ROTATE_90) { targetWidth = targetHeight targetHeight = Target.SIZE_ORIGINAL @@ -247,7 +254,15 @@ class PhotoFragment : ViewPagerFragment() { .load(getPathToLoad(medium)) .apply(options) .listener(object : RequestListener { - override fun onLoadFailed(e: GlideException?, model: Any?, target: Target?, isFirstResource: Boolean) = false + override fun onLoadFailed(e: GlideException?, model: Any?, target: Target?, isFirstResource: Boolean): Boolean { + if (!useHalfResolution && e?.rootCauses?.first() is OutOfMemoryError) { + useHalfResolution = true + Handler().post { + loadBitmap(degrees) + } + } + return false + } override fun onResourceReady(resource: Bitmap?, model: Any?, target: Target?, dataSource: DataSource?, isFirstResource: Boolean): Boolean { if (isFragmentVisible)