Android 如何在Kotlin中调整图像文件的大小

Android 如何在Kotlin中调整图像文件的大小,android,image,kotlin,Android,Image,Kotlin,我的应用程序拍摄照片并将其保存在手机中,但是当我的列表加载许多照片时,它会变得非常慢,这就是我需要降低照片分辨率的原因。我该怎么做 这就是我创作这幅画的方式 fun createImage():File{ val timeStamp = SimpleDateFormat("yyyyMMdd_HHmmss").format(Date()) val imageName = "JPEG_"+timeStamp+"_" var storageDir = g

我的应用程序拍摄照片并将其保存在手机中,但是当我的列表加载许多照片时,它会变得非常慢,这就是我需要降低照片分辨率的原因。我该怎么做

这就是我创作这幅画的方式

fun createImage():File{
        val timeStamp = SimpleDateFormat("yyyyMMdd_HHmmss").format(Date())
        val imageName = "JPEG_"+timeStamp+"_"
        var storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES)
        var image = File.createTempFile(imageName,".jpg",storageDir)

        currentPath=image.absolutePath
        return image
    }
  var beerPhoto: ImageView?=null
  ....
  viewHolder.beerPhoto?.setImageURI(Uri.parse(userDto.beerPhoto))
这就是我加载图片的方式

fun createImage():File{
        val timeStamp = SimpleDateFormat("yyyyMMdd_HHmmss").format(Date())
        val imageName = "JPEG_"+timeStamp+"_"
        var storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES)
        var image = File.createTempFile(imageName,".jpg",storageDir)

        currentPath=image.absolutePath
        return image
    }
  var beerPhoto: ImageView?=null
  ....
  viewHolder.beerPhoto?.setImageURI(Uri.parse(userDto.beerPhoto))

请使用Glide从服务器加载图像并调整其大小。我想这对你来说很简单也很有用

请查看下面的链接以了解有关调整图像大小的更多详细信息

存储库 {

mavenCentral()

谷歌()

}

依赖关系

{ 实现'com.github.bumptech.glide:glide:4.9.0'

annotationProcessor'com.github.bumptech.glide:compiler:4.9.0' }


您可以使用glide来调整图像大小和缩放,这非常简单

GlideApp  
    .with(context)
    .load(userDto.beerPhoto)
    .override(600, 200)  // it don't maintain aspect ratio 
    .into(beerPhoto);
要保持纵横比,可以使用显式缩放图像

GlideApp  
    .with(context)
    .load(userDto.beerPhoto)
    .override(600, 200)
    .centerCrop() // or use fitcenter
    .into(beerPhoto);


Thx,这个很好用!只有一个问题,我有一个kt类和一个没有活动的布局,我应该在上下文中放置什么呢?craete一个类扩展你的应用程序类,在其中你有一个静态方法getApplicationContext,你可以调用它来获取上下文
伴随对象{lateinit var yourApplication:App fun getApplicationContext():Context{return yourApplication.applicationContext}
解决方案是使用Glide,thx