36 lines
1.1 KiB
Kotlin
36 lines
1.1 KiB
Kotlin
package com.sw.inbound.utils
|
|
|
|
import android.content.Context
|
|
import android.graphics.Bitmap
|
|
import android.graphics.BitmapFactory
|
|
import android.net.Uri
|
|
import java.io.File
|
|
|
|
object ImageUtil {
|
|
fun uriToBitmap(context: Context, uri: Uri): Bitmap? {
|
|
return try {
|
|
val options = BitmapFactory.Options()
|
|
//options.inSampleSize = 2; // 这会将图片的尺寸缩小到原来的1/2
|
|
options.inJustDecodeBounds = false
|
|
// options.inPreferredConfig = Bitmap.Config.ARGB_8888
|
|
options.inPreferredConfig = Bitmap.Config.RGB_565
|
|
context.contentResolver.openInputStream(uri)?.use { stream ->
|
|
BitmapFactory.decodeStream(stream, null, options)
|
|
// BitmapFactory.decodeStream(stream)
|
|
}
|
|
} catch (e: Exception) {
|
|
e.printStackTrace()
|
|
null
|
|
}
|
|
}
|
|
|
|
// 直接解码文件路径
|
|
fun decodeFromFile(file: File): Bitmap? {
|
|
return if (file.exists()) {
|
|
BitmapFactory.decodeFile(file.absolutePath)
|
|
} else {
|
|
null
|
|
}
|
|
}
|
|
|
|
} |