From c3a7068806311eb4bb34625e1f8b94f488e1c7cc Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 28 Jan 2018 21:15:36 +0100 Subject: [PATCH] store only 80 media files in shared prefs and catch OOMs --- .../gallery/activities/MainActivity.kt | 3 ++- .../gallery/activities/MediaActivity.kt | 13 +++++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt index 82d85fffd..6bb06015e 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt @@ -239,8 +239,9 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { } private fun getDirectories() { - if (mIsGettingDirs) + if (mIsGettingDirs) { return + } mIsGettingDirs = true val dirs = getCachedDirectories() diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt index e9b36642f..0be1d39e6 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt @@ -42,7 +42,7 @@ import java.io.File import java.io.IOException class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { - private val SAVE_MEDIA_CNT = 100 + private val SAVE_MEDIA_CNT = 80 private val LAST_MEDIA_CHECK_PERIOD = 3000L private var mPath = "" @@ -611,9 +611,14 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { private fun storeFolder() { if (!config.temporarilyShowHidden) { - val subList = mMedia.subList(0, Math.min(SAVE_MEDIA_CNT, mMedia.size)) - val json = Gson().toJson(subList) - config.saveFolderMedia(mPath, json) + Thread { + try { + val subList = mMedia.subList(0, Math.min(SAVE_MEDIA_CNT, mMedia.size)) + val json = Gson().toJson(subList) + config.saveFolderMedia(mPath, json) + } catch (ignored: OutOfMemoryError) { + } + }.start() } }