When the user selects an image from the gallery, I convert it into a base64 string. Then I use an API. The image size must be less than 2MB, so if the image from the gallery is bigger than 2MB, I need to reduce its size. How should I do that?
That's my code:
// I pass the URI of the image to the method
@Override
protected Integer doInBackground(Uri... uris) {
InputStream inputStream = null;
try {
inputStream = getContentResolver().openInputStream(uris[0]);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
byte[] buffer = new byte[8192];
int bytesRead;
ByteArrayOutputStream output = new ByteArrayOutputStream();
try {
while ((bytesRead = inputStream.read(buffer)) != -1) {
output.write(buffer, 0, bytesRead);
}
} catch (IOException e) {
e.printStackTrace();
}
bytes = output.toByteArray();
String encodedImage = Base64.encodeToString(bytes, Base64.NO_WRAP);