将照片上传到Firebase存储时,如何减小照片的大小?

发布于 2025-01-24 18:23:30 字数 1661 浏览 3 评论 0原文

将照片上传到Firebase存储时,我想减小照片的大小。我的代码中有一个错误。我写了一种尺寸的方法。你能帮忙吗? (Kotlin)

auth.createUserWithEmailAndPassword(email, password).addOnCompleteListener { task ->
        if (task.isSuccessful && task.isComplete) {
            val userEmailInfo = auth.currentUser!!.email
            val currentUser = auth.currentUser
            currentUser.let { firebaseUser ->
                val storage = Firebase.storage
                val reference = storage.reference
                val userPicRef = reference.child(userEmailInfo!!).child("profilPicture")

                userPicRef.putFile(selectedPicture!!).addOnSuccessListener { taskSnapshot ->

                    val uploadPicRef = storage.reference.child(userEmailInfo).child("profilPicture")
                    uploadPicRef.downloadUrl.addOnSuccessListener {
                        val profilPicUrl = it.toString()
                        val userPic = hashMapOf<String, Any>()
                        userPic["profilPic"] = profilPicUrl
                    }
                }

第二。

private fun makeSmallerBitmap(image: Bitmap, maximumSize: Int): Bitmap {
    var width = image.width
    var height = image.height

    val bitmapRatio: Double = width.toDouble() / height.toDouble()
    if (bitmapRatio > 1) {
        width = maximumSize
        val scaledHeight = width / bitmapRatio
        height = scaledHeight.toInt()
    } else {
        height = maximumSize
        val scaledWidth = height * bitmapRatio
        width = scaledWidth.toInt()
    }
    return Bitmap.createScaledBitmap(image, width, height, true)

}

I want to reduce the size of the photo when uploading photos to Firebase storage. There is an error in my codes. I wrote a method for sizing. Can you help? (kotlin)

auth.createUserWithEmailAndPassword(email, password).addOnCompleteListener { task ->
        if (task.isSuccessful && task.isComplete) {
            val userEmailInfo = auth.currentUser!!.email
            val currentUser = auth.currentUser
            currentUser.let { firebaseUser ->
                val storage = Firebase.storage
                val reference = storage.reference
                val userPicRef = reference.child(userEmailInfo!!).child("profilPicture")

                userPicRef.putFile(selectedPicture!!).addOnSuccessListener { taskSnapshot ->

                    val uploadPicRef = storage.reference.child(userEmailInfo).child("profilPicture")
                    uploadPicRef.downloadUrl.addOnSuccessListener {
                        val profilPicUrl = it.toString()
                        val userPic = hashMapOf<String, Any>()
                        userPic["profilPic"] = profilPicUrl
                    }
                }

second.

private fun makeSmallerBitmap(image: Bitmap, maximumSize: Int): Bitmap {
    var width = image.width
    var height = image.height

    val bitmapRatio: Double = width.toDouble() / height.toDouble()
    if (bitmapRatio > 1) {
        width = maximumSize
        val scaledHeight = width / bitmapRatio
        height = scaledHeight.toInt()
    } else {
        height = maximumSize
        val scaledWidth = height * bitmapRatio
        width = scaledWidth.toInt()
    }
    return Bitmap.createScaledBitmap(image, width, height, true)

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文