Android 如何将bytearray与kotlin中的文件.length()进行比较?

Android 如何将bytearray与kotlin中的文件.length()进行比较?,android,file,kotlin,Android,File,Kotlin,我只需要发送的文件,只有不到2 mb。我使用文件和位图。在项目中使用easyimage库 如何比较常规imagefile.length()在除以1024时返回的值(kb)。但是位图图像返回byte bitmap.bytecount 如何获取位图文件的kb值?它会循环,直到完全压缩到0质量 代码: var质量=90 var imgKiloByteLength = imageFile.length() / 1024 var imgMegaByteLength = imgKiloByteLength

我只需要发送的文件,只有不到2 mb。我使用文件和位图。在项目中使用easyimage库

  • 如何比较常规imagefile.length()在除以1024时返回的值(kb)。但是位图图像返回byte bitmap.bytecount

  • 如何获取位图文件的kb值?它会循环,直到完全压缩到0质量

  • 代码: var质量=90

    var imgKiloByteLength = imageFile.length() / 1024
    var imgMegaByteLength = imgKiloByteLength / 1024  //returns in mb
    
    if (imgKiloByteLength > 2048) {
       while (imgKiloByteLength > 2048) {  //reducing the quality until it comes under 2mb
         var compressedBitmap = compressBitmap(bitmap, quality)
         bitmap = compressedBitmap
         Log.e("result",bitmap.getByteCount().toString())
         Glide.with(this@EditProfileActivity).load(bitmap).into(profileImage)
         Log.e("result", "image more than 2mb ${imgKiloByteLength}")
         quality -= 10
       }
    } else {
       Glide.with(this@EditProfileActivity).load(bitmap).into(profileImage)
       Log.e("result", "image size ok ${imgKiloByteLength}")
    }
    
    我能做什么?
    提前感谢。

    此代码可以帮助您将位图文件压缩到最大大小

    object BitmapUtils {
        const val ONE_KIO = 1024
        const val ONE_MIO = ONE_KIO * ONE_KIO
    
        /**
         * Compress, if needed, an image file to be lower than or equal to 1 Mio
         *
         * @param filePath Image file path
         *
         * @return Stream containing data of the compressed image. Can be null
         */
        fun compressedImageFile(filePath: String): InputStream? {
            var quality = 100
            var inputStream: InputStream? = null
            if (filePath.isNotEmpty()) {
                var bufferSize = Integer.MAX_VALUE
                val byteArrayOutputStream = ByteArrayOutputStream()
                try {
                    val bitmap = BitmapFactory.decodeFile(filePath)
                    do {
                        if (bitmap != null) {
                            byteArrayOutputStream.reset()
                            bitmap.compress(Bitmap.CompressFormat.JPEG, quality, byteArrayOutputStream)
                            bufferSize = byteArrayOutputStream.size()
                            logD { "quality: $quality -> length: $bufferSize" }
                            quality -= 10
                        }
                    } while (bufferSize > ONE_MIO)
                    inputStream = ByteArrayInputStream(byteArrayOutputStream.toByteArray())
                    byteArrayOutputStream.close()
                } catch (e: Exception) {
                    logE { "Exception when compressing file image: ${e.message}" }
                }
            }
            return inputStream
        }
    }
    

    不要关心
    logX
    方法,它们是我方便的日志方法

    我看不到文件。我不明白你想把它发送到哪里。也许你在这里找到了问题3的答案。你没有在while循环中更新imgkilobytellength,因此imgkilobytellength的值将保持与循环开始前相同。这意味着它是一个无限循环。