首页 > 解决方案 > 在 Android 中将图像压缩到 200KB 字节大小

问题描述

我尝试使用我的代码将大于 3MB的大尺寸图像压缩200BK。但它不起作用。尽管我把任何质量,输出总是具有相同的结果。我不知道我错在哪里。 请告诉我我的错误。 我只想转换PNG。MAX_SIZE_UPLOAD = 200KB。前任:

input: 4900000, quality: 90 output = 32000000
input: 4900000, quality: 80 output = 32000000
override fun compress(file: File): Single<File> {
        val result: SingleSubject<File> = SingleSubject.create()
        val options = RequestOptions()
        val optionsBitmap = BitmapFactory.Options()
        val originSize = file.length()
        optionsBitmap.inJustDecodeBounds = true
        BitmapFactory.decodeFile(file.absolutePath, optionsBitmap)
        Glide.with(context)
            .asBitmap().load(file)
            .into(object : SimpleTarget<Bitmap>() {
                override fun onLoadFailed(errorDrawable: Drawable?) {
                    super.onLoadFailed(errorDrawable)
                }

                override fun onResourceReady(
                    resource: Bitmap,
                    transition: com.bumptech.glide.request.transition.Transition<in Bitmap>?
                ) {
                    thread {
                        try {
                            val stream = ByteArrayOutputStream()
                            val quality = ((100 * MAX_SIZE_UPLOAD) / file.length())
                            resource.compress(Bitmap.CompressFormat.PNG, quality.toInt(), stream)
                            saveFileToCacheDir(stream.toByteArray())
                                .observeOnUiThread()
                                .subscribe({
                                    result.onSuccess(it)
                                }, {
                                    result.onError(Throwable())
                                })
                        } catch (e: Exception) {
                            result.onError(Throwable())
                        }
                    }
                }

            })
        return result
    }

    override fun saveFileToCacheDir(data: ByteArray): Single<File> {
        val result: SingleSubject<File> = SingleSubject.create()
        try {
            val file = File(context.cacheDir, "$FILE_NAME${System.currentTimeMillis()}")
            file.createNewFile()
            val fos = FileOutputStream(file)
            fos.write(data)
            fos.flush()
            fos.close()
            result.onSuccess(file)
        } catch (e: IOException) {
            result.onError(e)
        }
        return result
    }

标签: androidkotlinbitmapimage-compression

解决方案


这是将图像位图压缩到文件的解决方案。

fun bitmapToFile(bitmap: Bitmap, context: Context): String {
// Get the context wrapper
val wrapper = ContextWrapper(context)

// Initialize a new file instance to save bitmap object
var file = wrapper.getDir("Images", Context.MODE_PRIVATE)
file = File(file, "${UUID.randomUUID()}.jpg")

try {
    val stream: OutputStream = FileOutputStream(file)
    bitmap.compress(Bitmap.CompressFormat.JPEG,15,stream)
    stream.flush()
    stream.close()
} catch (e: IOException) {
    e.printStackTrace()
}

// Return the saved bitmap uri
return file.absolutePath

}


推荐阅读