Je développe une Android qui télécharge Base64
Chaîne d'image sur le serveur Web. Par conséquent, j'ai utilisé le code suivant pour convertir un bitmap en Base64
Chaîne.
public String getEncoded64ImageStringFromBitmap(Bitmap bitmap) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteFormat = stream.toByteArray();
String imgString = Base64.encodeToString(byteFormat, Base64.NO_WRAP);
return imgString;
}
Par exemple, j'ai besoin de convertir cela image . Après la conversion, je ne reçois pas le Base64
Chaîne comme je m'y attendais.
iVBORw0KGgoAAAANSUhEUgAAAIcAAAB7CAYAAABet5NoAAAABHNCSVQICAgIfAhkiAAAB/FJREFUeJzt3U1a4lgXB/D/uVQps3IBNuoKZEg56fQKml6BqRU0NdDXITWrBwbtDhp3oDuAicUQVoBYLkBmSndy3gHEDkgggZvkJjm/kR8hXPP8Pbk55IOQkJHVOCi97lnMqBJxFUwHAH4NWp6AMQMPAB4YeFBQ3V/637tJjTcuT2cXVYfJUqBjZq4CqAL4tOFlPRA/M9Mgye1Aca786eyiyqxsZrYAnGpabY8Jty6r25P+9wdN64zV4+fLOhh1AHVsDkJYsW8H7eEY1a6OFVxbATYDR7rXv6QHcKfSb3difp/IRrWr4xLcJvQGYjXiO4A6lR+tW62r1bUi38Y417XOsGa7IG6aEJKftSuLZ9shcJcZF93bYedwjKzGwYfX/SYz/6ljQLsgYMyEhu7/oDDS/OdYRsAYUPauc5OdwjHfl3YQd9mMrudA2UnNSca1yyYBDZi2HYjvnP1/7JPu9fNWL9/mRbMjj48dMP2+zesTMgG4EeeuZl4tbqFvsh2HCUHVt6kikcPxdHZRZZduE5hs6nLjlKeNbf97ghhcNVdi4NtRv9WM8ppI4XisXdgAXSMjG8Rn6JSnlq6AjD9fNojxl451Jeym0m/ZYRcOHY55MP7eZkQmIGBMiuuH9+3BLut5rF12YMCkcwc9pzyth/lHUWHWlvVgAAADR65L3aezi+q268hBMADg19LLXjfMghvDkYdg+HzaNiA5C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Jggg==
Quel est le problème dans ma conversion? Merci !
public static String encodeTobase64(Bitmap image) {
Bitmap immagex = image;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
immagex.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] b = baos.toByteArray();
String imageEncoded = Base64.encodeToString(b, Base64.DEFAULT);
return imageEncoded;
}
public static Bitmap decodeBase64(String input) {
byte[] decodedByte = Base64.decode(input, 0);
return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
}
utilisez l'extrait de code suivant pour convertir bitmap en tableau d'octets:
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream .toByteArray();
puis pour encoder base64 à partir de tableau d'octets utilisez l'instruction suivante:
String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);
public String getBase64ImageString(Bitmap photo) {
String imgString;
if(photo != null) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
byte[] profileImage = outputStream.toByteArray();
imgString = Base64.encodeToString(profileImage,
Base64.NO_WRAP);
}else{
imgString = "";
}
return imgString;
}
Passez votre Bitmap dans les paramètres vous obtiendrez votre chaîne d'image.