简体   繁体   中英

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

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?

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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