简体   繁体   中英

resize and compress image on upload from gallery

i want compress image on upload from gallery. i use "id.zelory:compressor" library for this purpose.

            case REQUEST_GALLERY_CODE: {
            if (resultCode == RESULT_OK && null != data) {
                try {
                    InputStream inputStream = getContentResolver().openInputStream(data.getData());
                    String type = getFileExtension(data.getData());
                    UploadImage(getBytes(inputStream) , type);

and getbytes function :

    private byte[] getBytes(InputStream is) throws IOException {
    ByteArrayOutputStream byteBuff = new ByteArrayOutputStream();
    int buffSize = 1024;
    byte[] buff = new byte[buffSize];
    int len = 0;
    while ((len = is.read(buff)) != -1) {
        byteBuff.write(buff, 0, len);
    }
    return byteBuff.toByteArray();
}

i want before send image to upload (UploadImage()) compress it by using this :

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

but input this code is actualImageFile! how can do this?

You can use native quality change. In my latest project I have used smth like this:

 val image = data?.extras?.get("data") as Bitmap
 val byteArrayOutputStream = ByteArrayOutputStream()
 image.compress(Bitmap.CompressFormat.PNG,IMAGE_QUALITY, byteArrayOutputStream)
 val byteArray = byteArrayOutputStream.toByteArray()
 service.send(Item(Base64.encodeToString(byteArray, Base64.DEFAULT))

IMAGE_QUALITY is a number from 0-100. You can play with a number to see what quality will satisfy your needs

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