Convert image to base64 String and vice versa

Image to base64 String

To convert image to base64 String use the code given below.
1. Define a new ByteArrayOutputStream.
java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();

2. Convert image in ImageView to Bitmap.
Bitmap bm = ((android.graphics.drawable.BitmapDrawable) imageview1.getDrawable()).getBitmap();

Or convert image in drawable folder to Bitmap.
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.image);

3. Convert bitmap image to byte array and encode it to String.
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String imageString = android.util.Base64.encodeToString(imageBytes, android.util.Base64.DEFAULT);

Base64 String to image

1. Decode String to Byte array
byte[] imageBytes = android.util.Base64.decode(imageString, android.util.Base64.DEFAULT);

2. Convert Byte array to Bitmap
Bitmap decodedImage = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);

3. Set the Bitmap as image of ImageView.
imageview1.setImageBitmap(decodedImage);

Here imageString is a base64 String.


Comments

  1. Base64 String to image doesn't work for me, image do not load

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
  2. Im having issues loading images as well.

    ReplyDelete
  3. I have figured out how to send and retrieve images to Firebase but it's showing the same image what am I missing? I'm trying to create a shopping application for a friend and this is a main part of the application, as it is sending a huge string of random numbers and integers.

    Thanks

    ReplyDelete
    Replies
    1. If you are Creating a shopping Application, you should use Android Studio or AIDE, and instead of converting image to String you should use Firebase Storage. You can get the codes for uploading image to Firebase Storage if you search google.

      Delete
  4. Hello, I have a doubt it will be possible to upload a file like a pdf or other files to firebase as well images, I would like you to upload a video referring to that or I explained how to do it thanks.Greetings from Nicaragua.

    ReplyDelete
  5. Online file converters are very useful and handy to your, take a look at this free online convert website. https://onlineconvertfree.com/

    ReplyDelete
  6. Can you make a video tutorial of this article please.....
    I wanna insert my images in imagestring but I don't know how..and yet you have an article of it but doesn't have any video or step by step tutorial of how to make it..

    ReplyDelete
  7. If you want a simple audio-to-text conversion for brief notes, transcribear Converter has a decent option. If you want to convert video to text, then you should visit their website.

    ReplyDelete
  8. stafaband has millions of results for free downloads of music, audio, podcasts, radio programs, and most notably their service is amazing.

    ReplyDelete
  9. Before upload any image, we should compress the image ​first..

    ReplyDelete

Post a Comment

Popular posts from this blog

Simple car racing android game in Sketchware

Creating a Drawing View in Sketchware

Enable Fullscreen for Youtube videos in WebView

How to enable upload from webview in Sketchware?

List of Calendar Format symbols valid in Sketchware