在Android中将图像压缩为200KB字节大小

问题描述 投票:0回答:1

我尝试用我的代码将大尺寸> 3MB 压缩 图像设置为200BK。但这行不通。尽管我提出了任何质量要求,但输出始终具有相同的结果。我不知道哪里错了。请告诉我我的错误。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
    }
android kotlin bitmap image-compression
1个回答
0
投票

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

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

}

© www.soinside.com 2019 - 2024. All rights reserved.