Home > front end >  I can't convert the image to Base 64
I can't convert the image to Base 64

Time:10-22

I do not understand why it marks me in red as an error. In both private String The error is in this line : return Base64.encodeToString(imageBytes, Base64.DEFAULT) , in encodeToString y DEFAULT

As error in both private strings it shows me this:

C:\Users\juan_\AndroidStudioProjects\OttavisHotelCafe\app\src\main\java\com\juanrichard\ottavishotelcafe\MainActivity_Registrar_Habitacion_H.java:488: error: cannot find symbol

  return Base64.encodeToString(imageBytes, Base64.DEFAULT);
                                                 ^
  symbol:   variable DEFAULT
  location: class Base64

private String GetStringImagen(Bitmap bitmap){
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG,100, baos);
    byte[ ]imageBytes = baos.toByteArray();

    return Base64.encodeToString(imageBytes, Base64.DEFAULT);
}

private String GetStringImagen(Bitmap bitmap) {
    String str = "";
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    byte [] imageBytes = baos.toByteArray();
    try {
        str = Base64.encodeToString(imageBytes, Base64.DEFAULT);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return str;
}

CodePudding user response:

For a Java project, from https://docs.oracle.com/javase/8/docs/api/java/util/Base64.Encoder.html#encodeToString-byte:A-, you don't give a second argument for a default encoding using the ISO-8859-1 charset. So, try using

return Base64.encodeToString(imageBytes);

OR, for Android, From https://developer.android.com/reference/android/util/Base64, you can put second argument as DEFAULT. So, try using

return Base64.encodeToString(imageBytes, DEFAULT);

as your return statement.

  • Related