6

Can bitmap be converted or saved to the webp format in android?

I search out for that but it turns out to be a dead end for me.

I need to save image to webp formate or else need to save in PNG/JPEG then convert to webp programmatically.

Vivek Faldu
  • 336
  • 3
  • 19

2 Answers2

11

You can use CompressFormat.WEBP to encode any bitmap in WEBP format.

Example:

FileOutputStream out = new FileOutputStream(path); 
bitmap.compress(Bitmap.CompressFormat.WEBP, 100, out);
out.close();
Nabin Bhandari
  • 15,949
  • 6
  • 45
  • 59
2

Since Bitmap.CompressFormat.WEBP is deprecated in Android 30, use as below

FileOutputStream(file).use { out ->
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
       bmp.compress(
       Bitmap.CompressFormat.WEBP_LOSSLESS, // or WEBP_LOSSY
       100,out)    
    } else {
       bmp.compress(Bitmap.CompressFormat.WEBP,100,out)
    }
}
Ahmet B.
  • 1,290
  • 10
  • 20