简体   繁体   English

在上传到 firebase 存储之前减小图像大小

[英]Reduce image size before uploading to firebase storage

I know there are many posts similar to this one, trust me I've tried lots of them, but it seems that for some reason this is not working for me.我知道有很多类似的帖子,相信我,我已经尝试了很多,但似乎由于某种原因这对我不起作用。 I want to upload a smaller size image to my firebase storage, this is my current code:我想将较小尺寸的图像上传到我的 firebase 存储,这是我当前的代码:

public fun uploadPictureToFirebaseStorage(context: Context, bitmap: Bitmap?,uri: Uri?,type :uploadType ) {
    val storage: FirebaseStorage = FirebaseStorage.getInstance()
    val storageRef: StorageReference = storage.getReference()
    val imgName = generateNewFileName()
    var riversRef : StorageReference? = null
    if(type == uploadType.POST) {
        riversRef =
                storageRef.child("images/posts/" + getUserName(context) + "/" + imgName)
    }
    else
    {
        riversRef =  storageRef.child("images/profiles/" + getUserName(context) )

    }
    var uploadTask : UploadTask? = null
    if (bitmap != null) {
        uploadTask = uploadFromBitmap(bitmap, riversRef)
    }
    else if(uri != null)
    {
        uploadTask = uploadFromUri(uri,riversRef,type)
    }
    uploadTask?.addOnFailureListener(OnFailureListener {
        Toast.makeText(context,"The image wasn't uploaded. Try again later!",Toast.LENGTH_LONG).show()
    })?.addOnSuccessListener(OnSuccessListener<Any?> {
        Toast.makeText(context,"The image was uploaded",Toast.LENGTH_LONG).show()
        if(type == uploadType.POST)
        {
            uploadPictureNameToDB(context,imgName)
            updateFriendFeed(getUserName(context)!!,imgName)
        }
    })
}

and the uploadFromBitmap function that's used in that is:以及其中使用的uploadFromBitmap function 是:

private fun uploadFromBitmap(bitmap: Bitmap, storageRef:StorageReference): UploadTask {
    val baos = ByteArrayOutputStream()
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos)
    val data: ByteArray = baos.toByteArray()
    return storageRef.putBytes(data)
}

Now, as many of the posts suggested, I have tried rescaling the image (using scale method), tried changing its extension to PNG and tried changing the quality from 100 to lower (like 20), however nothing worked.现在,正如许多帖子所建议的那样,我尝试重新缩放图像(使用缩放方法),尝试将其扩展名更改为 PNG,并尝试将质量从 100 更改为更低(如 20),但没有任何效果。 I will mention that the picture I tried these things on has the size of 248KB, and everything I changed kept upload it with that size.我会提到我尝试这些东西的图片大小为 248KB,我更改的所有内容都以该大小上传。
Is it possible that the resizing doesn't work on pictures lower than specific size?调整大小是否可能不适用于低于特定尺寸的图片?

Or if not, why is this not working I don't understand, is there something wrong with my code?或者如果没有,为什么这不起作用我不明白,我的代码有问题吗?

Okay I have solved the problem, all the methods were right, I just wasn't paying attention to the fact that the first function was entering through Uri and not through Bitmap (in the if else statement, the bitmap was null and the uri wasn't). Okay I have solved the problem, all the methods were right, I just wasn't paying attention to the fact that the first function was entering through Uri and not through Bitmap (in the if else statement, the bitmap was null and the uri wasn 't)。

So all I had to do was implying the changes to the uploadFromUri() function所以我所要做的就是暗示对uploadFromUri() function 的更改

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM