Kodowanie i dekodowanie obiektu bitmapowego w ciągu Base64 w Androidzie

Chcę zakodować i dekodować Bitmap obiekt w łańcuchu base64. Używam Androida API10,

Próbowałem, bez powodzenia, użyć metody w tej formie do zakodowania Bitmap.

public static String encodeTobase64(Bitmap image) {
    Bitmap immagex=image;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();  
    immagex.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    byte[] b = baos.toByteArray();
    String imageEncoded = Base64.encodeToString(b,Base64.DEFAULT);

    Log.e("LOOK", imageEncoded);
    return imageEncoded;
}
Author: Christoph, 2012-03-19

3 answers

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, 0);
    return BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.length);
}

Przykładowe użycie:

String myBase64Image = encodeToBase64(myBitmap, Bitmap.CompressFormat.JPEG, 100);
Bitmap myBitmapAgain = decodeBase64(myBase64Image);
 206
Author: Roman Truba,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-03-10 11:14:59

Mam nadzieję, że to ci pomoże

 Bitmap bitmap = BitmapFactory.decodeStream(this.getContentResolver().openInputStream(uri));

(jeśli odwołujesz się do URI do konstruowania bitmapy) Lub

Resources resources = this.getResources();
Bitmap bitmap= BitmapFactory.decodeResource(resources , R.drawable.logo);
W tym celu należy wykonać następujące czynności:]}

Następnie Zakoduj

 ByteArrayOutputStream stream = new ByteArrayOutputStream();  
 bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
 byte[] image = stream.toByteArray();
 String encodedImage = Base64.encode(image, Base64.DEFAULT);

Dla dekodowania logika będzie dokładnie odwrotna do kodowania

byte[] decodedString = Base64.decode(encodedImage, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length); 
 9
Author: Vipul Shah,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-11-17 06:16:07

Aby zakodować bimapę do obrazu:

 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 80, byteArrayOutputStream);
   byte[] imageBytes = byteArrayOutputStream.toByteArray();
   String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
    Log.d("bytearray", String.valueOf(byteArrayOutputStream.toByteArray()));
    Log.d("encodedimage",encodedImage);
 0
Author: Hanisha,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-07-12 07:08:19