1
  1. I just need to send file that is only less than 2 mb. I use file and bitmap. Used easyimage library in the project.

  2. How to compare the regular imagefile.length() returns in kb when i divide by 1024. But bitmap image returns byte bitmap.bytecount

  3. how to get values in kb of bitmap file? It loops until its totally compressed to 0 quality.

Code: var quality = 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}")
}

What are things that i can do? Thanks in advance.

Simson
  • 3,373
  • 2
  • 24
  • 38
PrakashKing
  • 635
  • 2
  • 11
  • 24
  • I dont see a file. And i dont understand where you wanna send it to. – blackapps Dec 11 '19 at 09:34
  • maybe you find an answer to question 3 here https://stackoverflow.com/questions/51919925/compress-bitmap-to-a-specific-byte-size-in-android/51920015#51920015 – leonardkraemer Dec 11 '19 at 09:37
  • 1
    you are not updating imgKiloByteLength within the while loop, so the value of imgKiloByteLength will remain the same as it was before the loop started. meaning it is an infinit loop. – Joachim Haglund Dec 11 '19 at 12:07

1 Answers1

1

This code may help you to compress a bitmap file to a maximum size

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
    }
}

Do not care about logX methods, they are my convenient log methods

Bruno
  • 3,872
  • 4
  • 20
  • 37