Fix Android 413 upload errors by resizing Bitmap dimensions with inSampleSize, reducing JPEG quality, and looping until the compressed file meets a target size limit.
An in-game support feature allowed players to send images, but large images were rejected with 413 Request Entity Too Large.
Resize the Bitmap and Reduce JPEG Quality
Use BitmapFactory.Options.inSampleSize to decode a smaller bitmap, then save it into the app cache as JPEG:
Decode the original bitmap and call bitmap.compress(Bitmap.CompressFormat.JPEG, 80, stream). This preserves dimensions but reduces JPEG quality. PNG does not support the same lossy quality setting.
Target a Maximum File Size
High-resolution images can remain too large. The following approach returns small originals unchanged, resizes large images to half dimensions, and lowers JPEG quality until the output is at most 300 KB or reaches the minimum quality: