0

I am trying to implement image compression for my file upload to Firebase Storage. Normal image choosing and upload works perfectly, but i would now like to implement image compression. I came across Zelory compressor and i am trying to implement but i can't seem to use the code correctly.

I am using this within a fragment and when i am trying to load the bitmap into the compressor line its seems to be giving an error on context:

error on code

My code before this is taking the ImageUri and converts it to a file path as needed by compressor.

val image_file_path = File(mImageUri.path)
val ctx = activity!!.applicationContext

val tempBitmap = Compressor.compress(ctx, image_file_path)

mImageUri is what i was using when just choosing and uploading as normal to firebase.

I have had a look at many examples of people trying to use this code and i can't to see what i am doing wrong. is it something to do with the fact that i am using it in a fragment? If so i would not mind changing my layout to just upload in the normal layout rather than a fragment.

I initially had the ctx input as just activity thinking that was the problem but this doesnt take away the error. any help?

Waseem Ahmed
  • 379
  • 1
  • 5
  • 17

1 Answers1

1

I resolved this by not using the Zelory Tool. It is complicated and found a more "standard" library to achieve the same.

The way to achieve the compression is to take your image, convert to Bitmap and follow the instructions in this link I found:

https://android--code.blogspot.com/2018/04/android-kotlin-compress-bitmap-example.html

It's honestly self explanatory, and the only difference i had to the code was that they use a static drawable whereas i had to convert my URI to Bitmap. i did this by using the following line:

val bitmap = MediaStore.Images.Media.getBitmap(activity?.contentResolver,mImageUri)

In the above, I use activity? because I was performing this operation in a fragment. Also, mImageUri is a companion object to which I assign the URI after selecting from the phones gallery.

The rest is as per the link.

Good luck to anyone else trying this. to give you a sense of the compression. going this route, I was able to compress a 8MB photo to 350kb using WEBP. Very efficient!

PS: When using WEBP, I managed to apply a 1% quality as per the above compression stated without much loss of quality. Even when downloading the compressed image on a PC, you would not say it was compressed.

Waseem Ahmed
  • 379
  • 1
  • 5
  • 17
  • I up-voted your answer and following same to implement in my code written in Kotlin and interacting with Firebase, but I've a small doubt, please have a look: https://stackoverflow.com/questions/62898708/kotlin-image-compression-implementation – Android Jul 14 '20 at 15:36
  • @Android, please see my answer on your post. i have kept some descriptions from my own code so use your discretion on what needs to be changed. i really hope you manage to use the code! do let me know if you succeed – Waseem Ahmed Jul 16 '20 at 07:49