使用BitmapFactory.decodeFileDescriptor 避免重复初始化FileInputStream

pull/957/head
ag2s20150909 4 years ago
parent cdda7c3191
commit 39debe1acc
  1. 3
      app/build.gradle
  2. 20
      app/src/main/java/io/legado/app/utils/BitmapUtils.kt

@ -178,7 +178,8 @@ dependencies {
implementation 'com.github.gedoor:rhino-android:1.5'
//
implementation 'com.squareup.okhttp3:okhttp:4.9.0'
//noinspection GradleDependency
implementation 'com.squareup.okhttp3:okhttp:4.9.1'
implementation 'com.ljx.rxhttp:rxhttp:2.5.7'
kapt 'com.ljx.rxhttp:rxhttp-compiler:2.5.7'

@ -28,11 +28,11 @@ object BitmapUtils {
*/
fun decodeBitmap(path: String, width: Int, height: Int): Bitmap? {
val op = BitmapFactory.Options()
op.inPreferredConfig=Config.RGB_565
var ips=FileInputStream(path)
op.inPreferredConfig = Config.RGB_565
val ips = FileInputStream(path)
// inJustDecodeBounds如果设置为true,仅仅返回图片实际的宽和高,宽和高是赋值给opts.outWidth,opts.outHeight;
op.inJustDecodeBounds = true
BitmapFactory.decodeStream(ips,null,op)//获取尺寸信息
BitmapFactory.decodeFileDescriptor(ips.fd, null, op)
//获取比例大小
val wRatio = ceil((op.outWidth / width).toDouble()).toInt()
val hRatio = ceil((op.outHeight / height).toDouble()).toInt()
@ -44,9 +44,8 @@ object BitmapUtils {
op.inSampleSize = hRatio
}
}
ips=FileInputStream(path)
op.inJustDecodeBounds = false
return BitmapFactory.decodeStream(ips,null,op)
return BitmapFactory.decodeFileDescriptor(ips.fd, null, op)
}
/** 从path中获取Bitmap图片
@ -54,15 +53,16 @@ object BitmapUtils {
* @return
*/
fun decodeBitmap(path: String): Bitmap? {
val opts = BitmapFactory.Options()
opts.inPreferredConfig=Config.RGB_565
var ips=FileInputStream(path)
opts.inPreferredConfig = Config.RGB_565
val ips = FileInputStream(path)
opts.inJustDecodeBounds = true
BitmapFactory.decodeStream(ips,null,opts)
BitmapFactory.decodeFileDescriptor(ips.fd, null, opts)
opts.inSampleSize = computeSampleSize(opts, -1, 128 * 128)
opts.inJustDecodeBounds = false
ips=FileInputStream(path)
return BitmapFactory.decodeStream(ips,null,opts)
return BitmapFactory.decodeFileDescriptor(ips.fd, null, opts)
}
/**

Loading…
Cancel
Save