Merge pull request #1785 from ag2s20150909/master

使用decodeFileDescriptor替换decodeFile 避免两次创建FileInputStream,自动关闭FileInputStream。
pull/1786/head
kunfei 3 years ago committed by GitHub
commit bd5cd2a421
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      app/src/main/java/io/legado/app/help/http/cronet/CronetInterceptor.kt
  2. 82
      app/src/main/java/io/legado/app/utils/BitmapUtils.kt

@ -48,8 +48,8 @@ class CronetInterceptor(private val cookieJar: CookieJar?) : Interceptor {
} else { } else {
OldCallback(request, call) OldCallback(request, call)
} }
buildRequest(request, callBack)?.let { buildRequest(request, callBack)?.runCatching {
return callBack.waitForDone(it) return callBack.waitForDone(this)
} }
return null return null
} }

@ -9,6 +9,7 @@ import android.graphics.BitmapFactory
import android.graphics.Color import android.graphics.Color
import android.graphics.Matrix import android.graphics.Matrix
import com.google.android.renderscript.Toolkit import com.google.android.renderscript.Toolkit
import java.io.FileInputStream
import java.io.IOException import java.io.IOException
import kotlin.math.* import kotlin.math.*
@ -27,39 +28,51 @@ object BitmapUtils {
*/ */
@Throws(IOException::class) @Throws(IOException::class)
fun decodeBitmap(path: String, width: Int, height: Int): Bitmap { fun decodeBitmap(path: String, width: Int, height: Int): Bitmap {
val op = BitmapFactory.Options()
// inJustDecodeBounds如果设置为true,仅仅返回图片实际的宽和高,宽和高是赋值给opts.outWidth,opts.outHeight; val fis = FileInputStream(path)
op.inJustDecodeBounds = true return fis.use {
BitmapFactory.decodeFile(path, op) val op = BitmapFactory.Options()
//获取比例大小 // inJustDecodeBounds如果设置为true,仅仅返回图片实际的宽和高,宽和高是赋值给opts.outWidth,opts.outHeight;
val wRatio = ceil((op.outWidth / width).toDouble()).toInt() op.inJustDecodeBounds = true
val hRatio = ceil((op.outHeight / height).toDouble()).toInt() BitmapFactory.decodeFileDescriptor(fis.fd, null, op)
//如果超出指定大小,则缩小相应的比例 //获取比例大小
if (wRatio > 1 && hRatio > 1) { val wRatio = ceil((op.outWidth / width).toDouble()).toInt()
if (wRatio > hRatio) { val hRatio = ceil((op.outHeight / height).toDouble()).toInt()
op.inSampleSize = wRatio //如果超出指定大小,则缩小相应的比例
} else { if (wRatio > 1 && hRatio > 1) {
op.inSampleSize = hRatio if (wRatio > hRatio) {
op.inSampleSize = wRatio
} else {
op.inSampleSize = hRatio
}
} }
op.inJustDecodeBounds = false
BitmapFactory.decodeFileDescriptor(fis.fd, null, op)
} }
op.inJustDecodeBounds = false
return BitmapFactory.decodeFile(path, op)
} }
@Throws(IOException::class) @Throws(IOException::class)
fun decodeBitmap(path: String, width: Int): Bitmap { fun decodeBitmap(path: String, width: Int): Bitmap {
val op = BitmapFactory.Options()
// inJustDecodeBounds如果设置为true,仅仅返回图片实际的宽和高,宽和高是赋值给opts.outWidth,opts.outHeight; val fis = FileInputStream(path)
op.inJustDecodeBounds = true
BitmapFactory.decodeFile(path, op) return fis.use {
//获取比例大小 val op = BitmapFactory.Options()
val wRatio = ceil((op.outWidth / width).toDouble()).toInt() // inJustDecodeBounds如果设置为true,仅仅返回图片实际的宽和高,宽和高是赋值给opts.outWidth,opts.outHeight;
//如果超出指定大小,则缩小相应的比例 op.inJustDecodeBounds = true
if (wRatio > 1) {
op.inSampleSize = wRatio BitmapFactory.decodeFileDescriptor(fis.fd, null, op)
//获取比例大小
val wRatio = ceil((op.outWidth / width).toDouble()).toInt()
//如果超出指定大小,则缩小相应的比例
if (wRatio > 1) {
op.inSampleSize = wRatio
}
op.inJustDecodeBounds = false
BitmapFactory.decodeFileDescriptor(fis.fd, null, op)
} }
op.inJustDecodeBounds = false
return BitmapFactory.decodeFile(path, op)
} }
/** 从path中获取Bitmap图片 /** 从path中获取Bitmap图片
@ -68,12 +81,17 @@ object BitmapUtils {
*/ */
@Throws(IOException::class) @Throws(IOException::class)
fun decodeBitmap(path: String): Bitmap { fun decodeBitmap(path: String): Bitmap {
val opts = BitmapFactory.Options() val fis = FileInputStream(path)
opts.inJustDecodeBounds = true return fis.use {
BitmapFactory.decodeFile(path, opts) val opts = BitmapFactory.Options()
opts.inSampleSize = computeSampleSize(opts, -1, 128 * 128) opts.inJustDecodeBounds = true
opts.inJustDecodeBounds = false
return BitmapFactory.decodeFile(path, opts) BitmapFactory.decodeFileDescriptor(fis.fd, null, opts)
opts.inSampleSize = computeSampleSize(opts, -1, 128 * 128)
opts.inJustDecodeBounds = false
BitmapFactory.decodeFileDescriptor(fis.fd, null, opts)
}
} }
/** /**

Loading…
Cancel
Save