Paula Daniela
Paula Daniela

Reputation: 115

How to re-size image before uploading it to Firebase?

I saw this question before but none of the solutions really suited my case.

I would like to save time uplading/downloading content from Firebase Storage in addition to save some space. Since firebase uses the image uri, I couldn't think of a proper way to achieve this.

A solution may be creating a "copy" to the cache or some temporary file to resize, crop, etc,then upload that copy to Storage. However I don't know if it is a good approach and I don't really know how to do it.

Would you suggest some ways of doing this? I really appreciate examples.

Upvotes: 2

Views: 3253

Answers (3)

mu'ath Fathi
mu'ath Fathi

Reputation: 1

You can use this library

Gradle

dependencies {
        implementation 'com.github.WindSekirun:MediaResizer:1.0.0' 
}

allprojects {
    repositories {
     maven { url 'https://jitpack.io' }
    }

Compress Image

ImageResizeOption resizeOption = new ImageResizeOption.Builder()
            .setImageProcessMode(ImageMode.ResizeAndCompress)
            .setImageResolution(1280, 720)
            .setBitmapFilter(false)
            .setCompressFormat(Bitmap.CompressFormat.JPEG)
            .setCompressQuality(75)
            .setScanRequest(ScanRequest.TRUE)
            .build();

ResizeOption option = new ResizeOption.Builder()
            .setMediaType(MediaType.IMAGE)
            .setImageResizeOption(resizeOption)
            .setTargetPath(path)
            .setOutputPath(imageFile.getAbsolutePath())
            .setCallback((code, output) -> {
                txtStatus.setText(ResultBuilder.displayImageResult(code, path, output));
                progress.dismiss();
            }).build();

MediaResizer.process(option);

Upvotes: 0

Yupi
Yupi

Reputation: 4470

You could use some of the custom made libraries for compresing media files like images and videos. For example: Compressor is good library for compressing images: https://github.com/zetbaitsu/Compressor

To compress image and upload on Firebase Storage you could simply do something like this:

try {
                Bitmap bitmap = new Compressor(this)
                        .setMaxHeight(200) //Set height and width
                        .setMaxWidth(200)
                        .setQuality(100) // Set Quality
                        .compressToBitmap(file);
            } catch (IOException e) {
                e.printStackTrace();
            }

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
            final byte[] bytes = baos.toByteArray();

And then when calling and setting UploadTask:

UploadTask uploadTask = StorageReference.putBytes(bytes);

Of course there are some other libraries like:

SiliCompressor

https://github.com/Tourenathan-G5organisation/SiliCompressor

Upvotes: 5

Rahul Singh Chandrabhan
Rahul Singh Chandrabhan

Reputation: 2496

Use a third party library, It's easy and efficient

Gradle

dependencies {
    compile 'id.zelory:compressor:2.1.0'
}

Compress Image File

compressedImageFile = new Compressor(this).compressToFile(actualImageFile);

Compress Image File to Bitmap

compressedImageBitmap = new Compressor(this).compressToBitmap(actualImageFile);

For more information visit this

https://github.com/zetbaitsu/Compressor

Hope this helps as I always use this library.

Upvotes: 4

Related Questions