use decodeFileDescriptor replace decodeFile to avoid twice create FileInputStream and auto close FileInputStream

pull/1785/head
ag2s20150909 2 years ago
parent cc1768bd66
commit 26d1bb56d3
  1. 4
      app/src/main/java/io/legado/app/help/http/cronet/CronetInterceptor.kt
  2. 30
      app/src/main/java/io/legado/app/utils/BitmapUtils.kt

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

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

Loading…
Cancel
Save