How do I compress image in android studio(JAVA) and store it in device specific folder
Asked Answered
E

1

1

I am making an app for image compression And I am new to Android development....So my problem is as we have some issue with accessing External storage in JAVA......I have Tried

compile 'id.zelory:compressor:2.1.0'
compressedImage = new Compressor(this)
    .setMaxWidth(640)
    .setMaxHeight(480)
    .setQuality(75)
    .setCompressFormat(Bitmap.CompressFormat.WEBP)
    .setDestinationDirectoryPath(
        Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES
        ).getAbsolutePath()
    )
    .compressToFile(actualImage);

Okayy...so can I use WEBP_LOSSLESS instead of WEBP for Android API 30? and The main issue is how do I set the destination for storing in Specific Folder?

Ecuador answered 4/3, 2021 at 21:56 Comment(1)
I want to save compressed image in device as we can't use ExternalStorage public library in API 30 so how do I save it at particular directoryEcuador
S
1

The red deprecation box of getExternalStoragePublicDirectory lists alternatives:

Apps can continue to access content stored on shared/external storage by migrating to alternatives such as Context#getExternalFilesDir(String), MediaStore, or Intent#ACTION_OPEN_DOCUMENT.

Bitmap.CompressFormat.WEBP also contains a deprecation notice:

This field was deprecated in API level 30. in favor of the more explicit CompressFormat#WEBP_LOSSY and CompressFormat#WEBP_LOSSLESS.


For example, something like this should work:

Context ctx = this;
File storeFolder = ctx.getExternalFilesDir(Environment.DIRECTORY_PICTURES);

compressedImage = new Compressor(this)
    .setMaxWidth(640)
    .setMaxHeight(480)
    .setQuality(75)
    .setCompressFormat(Bitmap.CompressFormat.WEBP_LOSSLESS)
    .setDestinationDirectoryPath(
        storeFolder.getAbsolutePath()
    )
    .compressToFile(actualImage);
Secession answered 8/3, 2021 at 17:55 Comment(5)
I am new here Can you provide code for storeFolder?Ecuador
@AVITHAKUR I'm not sure I follow. Could you be more specific?Secession
yess...okay so basically I am making an android app for image compression and I wanted to use the zelory compressor as I am new to android development I was not sure how to use this compressor with API 29 & 30...But I have tried different approach as mentioned below...Ecuador
okay...I wanna post the code that I tried and its working but its too long for the comment...so should I post it as Question or what?Ecuador
@AVITHAKUR If you have any new information to add to your question, simply edit it.Secession

© 2022 - 2024 — McMap. All rights reserved.