Skip to content

Commit

Permalink
在解码大图时添加缩放限制,以减少内存溢出的可能
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaojieonly committed Nov 13, 2024
1 parent 8a0329c commit 61f3ac9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@
package com.hippo.ehviewer.gallery;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Process;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.hippo.a7zip.ArchiveException;
import com.hippo.ehviewer.EhApplication;
import com.hippo.ehviewer.GetText;
import com.hippo.ehviewer.R;
import com.hippo.lib.glgallery.GalleryPageView;
Expand Down Expand Up @@ -262,6 +265,13 @@ public void run() {
}

try {
// BitmapFactory.Options option = new BitmapFactory.Options();
// if (stream.available()>40960){
// option.inSampleSize = 4;
// }
// Bitmap bitmap = BitmapFactory.decodeStream(stream,null,option);
// BitmapDrawable drawable = new BitmapDrawable(EhApplication.getInstance().getResources(),bitmap);
// Image image = Image.decode(drawable, false);
Image image = Image.decode(BitmapDrawable.createFromStream(stream,null), false);
if (image != null) {
notifyPageSucceed(index, image);
Expand Down
34 changes: 24 additions & 10 deletions app/src/main/java/com/hippo/lib/image/Image.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
*/
package com.hippo.lib.image

import android.content.Context
import android.content.res.Resources
import android.graphics.Bitmap
import android.graphics.BitmapFactory
Expand All @@ -39,7 +38,6 @@ import com.google.firebase.crashlytics.FirebaseCrashlytics
import com.hippo.ehviewer.EhApplication
import java.io.FileInputStream
import java.nio.channels.FileChannel
import kotlin.Exception
import kotlin.math.min

class Image private constructor(
Expand All @@ -54,6 +52,10 @@ class Image private constructor(
init {
mObtainedDrawable = null
source?.let {
var simpleSize: Int? = null
if (source.available() > 5242880) {
simpleSize = source.available() / 5242880
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
val src = ImageDecoder.createSource(
source.channel.map(
Expand All @@ -70,10 +72,11 @@ class Image private constructor(
// Idk it will cause how much performance regression

decoder.setTargetSampleSize(
min(
info.size.width / (2 * screenWidth),
info.size.height / (2 * screenHeight)
).coerceAtLeast(1)
simpleSize
?: min(
info.size.width / (2 * screenWidth),
info.size.height / (2 * screenHeight)
).coerceAtLeast(1)
)
// Don't
}
Expand All @@ -82,7 +85,16 @@ class Image private constructor(
}
// Should we lazy decode it?
} else {
mObtainedDrawable=BitmapDrawable.createFromStream(source,null)
if (simpleSize != null) {
val option = BitmapFactory.Options().apply {
inSampleSize = simpleSize
}
val bitmap = BitmapFactory.decodeStream(source, null, option)
mObtainedDrawable =
BitmapDrawable(EhApplication.getInstance().resources, bitmap)
} else {
mObtainedDrawable = BitmapDrawable.createFromStream(source, null)
}
}
}
if (mObtainedDrawable == null) {
Expand All @@ -96,8 +108,10 @@ class Image private constructor(
} else {
mObtainedDrawable is AnimationDrawable
}
val width = (mObtainedDrawable as? BitmapDrawable)?.bitmap?.width ?: mObtainedDrawable!!.intrinsicWidth
val height = (mObtainedDrawable as? BitmapDrawable)?.bitmap?.height ?: mObtainedDrawable!!.intrinsicHeight
val width =
(mObtainedDrawable as? BitmapDrawable)?.bitmap?.width ?: mObtainedDrawable!!.intrinsicWidth
val height = (mObtainedDrawable as? BitmapDrawable)?.bitmap?.height
?: mObtainedDrawable!!.intrinsicHeight
val isRecycled = mObtainedDrawable == null

var started = false
Expand Down Expand Up @@ -215,7 +229,7 @@ class Image private constructor(
@JvmStatic
fun decode(drawable: Drawable?, hardware: Boolean = true): Image? {
try {
return Image(null,drawable, hardware = hardware)
return Image(null, drawable, hardware = hardware)
} catch (e: Exception) {
e.printStackTrace()
FirebaseCrashlytics.getInstance().recordException(e)
Expand Down

0 comments on commit 61f3ac9

Please sign in to comment.