Encode and decode bitmap object in base64 string in Android
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);
}
Example usage:
String myBase64Image = encodeToBase64(myBitmap, Bitmap.CompressFormat.JPEG, 100);
Bitmap myBitmapAgain = decodeBase64(myBase64Image);
How to convert a Base64 string into a Bitmap image to show it in a ImageView?
You can just basically revert your code using some other built in methods.
byte[] decodedString = Base64.decode(encodedImage, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
Android Bitmap to Base64 String
use following method to convert bitmap to byte array:
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream .toByteArray();
to encode base64 from byte array use following method
String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);
Convert base64 string to image in Android
//encode image(from image path) to base64 string
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Bitmap bitmap = BitmapFactory.decodeFile(pathOfYourImage);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String imageString = Base64.encodeToString(imageBytes, Base64.DEFAULT);
//encode image(image from drawable) to base64 string
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.yourDrawableImage);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String imageString = Base64.encodeToString(imageBytes, Base64.DEFAULT);
Related Topics
How to Design Any Screen Size and - Density in Android(Multi Screen for Mobiles in Android)
How to Get the Width and Height of an Image View in Android
Menuitemcompat.Getactionview Always Returns Null
Android - Best and Safe Way to Stop Thread
Select All Text Inside Edittext When It Gets Focus
Android - How to Make a Button Flash
Fragment Oncreateview and Onactivitycreated Called Twice
Buildconfig.Debug Always False When Building Library Projects with Gradle
Android: Get All Pendingintents Set with Alarmmanager
How to Synchronise Projects to Github with Android Studio
How to Set Up Dagger Dependency Injection from Scratch in Android Project
Custom Ssl Handling Stopped Working on Android 2.2 Froyo
How to Show Android Checkbox at Right Side
Android: Getting "Manifest Merger Failed" Error After Updating to a New Version of Gradle
How to Set Android_Home Path in Ubuntu
Updating the List View When the Adapter Data Changes
Why Does My App Size on Device Differ Than the APK or Play Store Size