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);
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.
Here imageString is a base64 String.
Base64 String to image doesn't work for me, image do not load
ReplyDeleteThis comment has been removed by the author.
DeleteIm having issues loading images as well.
ReplyDeleteI 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.
ReplyDeleteThanks
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.
DeleteHello, 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.
ReplyDeleteOnline file converters are very useful and handy to your, take a look at this free online convert website. https://onlineconvertfree.com/
ReplyDeleteCan you make a video tutorial of this article please.....
ReplyDeleteI 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..
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.
ReplyDeletestafaband has millions of results for free downloads of music, audio, podcasts, radio programs, and most notably their service is amazing.
ReplyDeleteBefore upload any image, we should compress the image first..
ReplyDelete