Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature/optimize-memory-cache] Implementation of Memory Cache Optimization for Image Picking in Android #36

Merged
merged 1 commit into from
Dec 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.LocalContext
import com.preat.peekaboo.image.picker.PeekabooBitmapCache.bitmapToByteArray
import com.preat.peekaboo.image.picker.SelectionMode.Companion.INFINITY
import kotlinx.coroutines.CoroutineScope
import java.io.ByteArrayOutputStream
Expand Down Expand Up @@ -166,11 +167,13 @@ private fun resizeImage(
width: Int,
height: Int,
): ByteArray? {
val cacheKey = "${uri}_w${width}_h$height"
PeekabooBitmapCache.instance.get(cacheKey)?.let { cachedBitmap ->
return bitmapToByteArray(cachedBitmap)
}

context.contentResolver.openInputStream(uri)?.use { inputStream ->
val options =
BitmapFactory.Options().apply {
inJustDecodeBounds = true
}
val options = BitmapFactory.Options().apply { inJustDecodeBounds = true }
BitmapFactory.decodeStream(inputStream, null, options)

var inSampleSize = 1
Expand All @@ -182,11 +185,12 @@ private fun resizeImage(
options.inSampleSize = inSampleSize

context.contentResolver.openInputStream(uri)?.use { scaledInputStream ->
val scaledBitmap = BitmapFactory.decodeStream(scaledInputStream, null, options)
if (scaledBitmap != null) {
BitmapFactory.decodeStream(scaledInputStream, null, options)?.let { scaledBitmap ->
ByteArrayOutputStream().use { byteArrayOutputStream ->
scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream)
return byteArrayOutputStream.toByteArray()
val byteArray = byteArrayOutputStream.toByteArray()
PeekabooBitmapCache.instance.put(cacheKey, scaledBitmap)
return byteArray
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2023 onseok
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.preat.peekaboo.image.picker

import android.graphics.Bitmap
import android.util.LruCache
import java.io.ByteArrayOutputStream

internal object PeekabooBitmapCache {
internal val instance: LruCache<String, Bitmap> by lazy {
LruCache<String, Bitmap>(calculateMemoryCacheSize())
}

private fun calculateMemoryCacheSize(): Int {
val maxMemory = Runtime.getRuntime().maxMemory() / 1024
val cacheSize = (maxMemory * 0.25).toInt()
return cacheSize.coerceAtLeast(1024 * 1024)
}

internal fun bitmapToByteArray(bitmap: Bitmap): ByteArray {
ByteArrayOutputStream().use { stream ->
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream)
return stream.toByteArray()
}
}
}