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 { } 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,10 +28,13 @@ 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 fis = FileInputStream(path)
return fis.use {
val op = BitmapFactory.Options() val op = BitmapFactory.Options()
// inJustDecodeBounds如果设置为true,仅仅返回图片实际的宽和高,宽和高是赋值给opts.outWidth,opts.outHeight; // inJustDecodeBounds如果设置为true,仅仅返回图片实际的宽和高,宽和高是赋值给opts.outWidth,opts.outHeight;
op.inJustDecodeBounds = true op.inJustDecodeBounds = true
BitmapFactory.decodeFile(path, op) BitmapFactory.decodeFileDescriptor(fis.fd, null, op)
//获取比例大小 //获取比例大小
val wRatio = ceil((op.outWidth / width).toDouble()).toInt() val wRatio = ceil((op.outWidth / width).toDouble()).toInt()
val hRatio = ceil((op.outHeight / height).toDouble()).toInt() val hRatio = ceil((op.outHeight / height).toDouble()).toInt()
@ -43,15 +47,22 @@ object BitmapUtils {
} }
} }
op.inJustDecodeBounds = false op.inJustDecodeBounds = false
return BitmapFactory.decodeFile(path, op) BitmapFactory.decodeFileDescriptor(fis.fd, null, op)
}
} }
@Throws(IOException::class) @Throws(IOException::class)
fun decodeBitmap(path: String, width: Int): Bitmap { fun decodeBitmap(path: String, width: Int): Bitmap {
val fis = FileInputStream(path)
return fis.use {
val op = BitmapFactory.Options() val op = BitmapFactory.Options()
// inJustDecodeBounds如果设置为true,仅仅返回图片实际的宽和高,宽和高是赋值给opts.outWidth,opts.outHeight; // inJustDecodeBounds如果设置为true,仅仅返回图片实际的宽和高,宽和高是赋值给opts.outWidth,opts.outHeight;
op.inJustDecodeBounds = true op.inJustDecodeBounds = true
BitmapFactory.decodeFile(path, op)
BitmapFactory.decodeFileDescriptor(fis.fd, null, op)
//获取比例大小 //获取比例大小
val wRatio = ceil((op.outWidth / width).toDouble()).toInt() val wRatio = ceil((op.outWidth / width).toDouble()).toInt()
//如果超出指定大小,则缩小相应的比例 //如果超出指定大小,则缩小相应的比例
@ -59,7 +70,9 @@ object BitmapUtils {
op.inSampleSize = wRatio op.inSampleSize = wRatio
} }
op.inJustDecodeBounds = false op.inJustDecodeBounds = false
return BitmapFactory.decodeFile(path, op) BitmapFactory.decodeFileDescriptor(fis.fd, null, 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 fis = FileInputStream(path)
return fis.use {
val opts = BitmapFactory.Options() val opts = BitmapFactory.Options()
opts.inJustDecodeBounds = true opts.inJustDecodeBounds = true
BitmapFactory.decodeFile(path, opts)
BitmapFactory.decodeFileDescriptor(fis.fd, null, opts)
opts.inSampleSize = computeSampleSize(opts, -1, 128 * 128) opts.inSampleSize = computeSampleSize(opts, -1, 128 * 128)
opts.inJustDecodeBounds = false opts.inJustDecodeBounds = false
return BitmapFactory.decodeFile(path, opts) BitmapFactory.decodeFileDescriptor(fis.fd, null, opts)
}
} }
/** /**

Loading…
Cancel
Save