I'm getting from a server a image in a String variable, that I need to reduce in the mobile and reupload. I have the next functions but I don't know why it takes so much time, I really don't know if it is blocked/freezed on some point or it takes so much time for the process.
public static String encodeToBase64(Bitmap image, Bitmap.CompressFormat compressFormat, int quality)
{
ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
image.compress(compressFormat, quality, byteArrayOS);
return Base64.encodeToString(byteArrayOS.toByteArray(), Base64.DEFAULT);
}
public static Bitmap decodeBase64(String input)
{
byte[] decodedBytes = Base64.decode(input, Base64.DEFAULT);
return BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.length);
}
public static String compressImage(String base64ImageData){
long SIZE_1_5_MB = 1572864;
int QUALITY = 100;
Bitmap decodedByte = decodeBase64(base64ImageData);
StringBuilder sb = new StringBuilder();
do{
sb.setLength(0);
sb = new StringBuilder();
String eBase64 = encodeToBase64(decodedByte, Bitmap.CompressFormat.JPEG, QUALITY);
sb.append(eBase64);
Log.e("IMAGE", "QUALITY: " + QUALITY + " SIZE: " + sb.length());
QUALITY = QUALITY - 5;
}while (sb.length() > SIZE_1_5_MB);
return sb.toString();
}
I want to reduce the photos to a size less than 1,5 mb