Asked  7 Months ago    Answers:  5   Viewed   73 times

What is the code to transform an image (maximum of 200 KB) into a Base64 String?

I need to know how to do it with Android, because I have to add the functionality to upload images to a remote server in my main app, putting them into a row of the database, as a string.

I am searching in Google and in Stack Overflow, but I could not find easy examples that I can afford and also I find some examples, but they are not talking about to transform into a String. Then I need to transform into a string to upload by JSON to my remote server.

 Answers

95

You can use the Base64 Android class:

String encodedImage = Base64.encodeToString(byteArrayImage, Base64.DEFAULT);

You'll have to convert your image into a byte array though. Here's an example:

Bitmap bm = BitmapFactory.decodeFile("/path/to/image.jpg");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); // bm is the bitmap object
byte[] b = baos.toByteArray();

* Update *

If you're using an older SDK library (because you want it to work on phones with older versions of the OS) you won't have the Base64 class packaged in (since it just came out in API level 8 AKA version 2.2).

Check this article out for a workaround:

How to base64 encode decode Android

Tuesday, June 1, 2021
 
brombeer
answered 7 Months ago
11

You can use the HTML5 <canvas> for it:

Create a canvas, load your image into it and then use toDataURL() to get the Base64 representation (actually, it's a data: URL, but it contains the Base64-encoded image).

Tuesday, June 1, 2021
 
Neysor
answered 7 Months ago
22

The most straightforward way would be to modify the RTF code to insert the picture yourself.

In RTF, a picture is defined like this:

'{' pict (brdr? & shading? & picttype & pictsize & metafileinfo?) data '}' A question mark indicates the control word is optional. "data" is simply the content of the file in hex format. If you want to use binary, use the bin control word.

For instance:

{pictpngblippicw10449pich3280picwgoal5924pichgoal1860 hex data}
{pictpngblippicw10449pich3280picwgoal5924pichgoal1860bin binary data}

pict = starts a picture group, pngblip = png picture picwX = width of the picture (X is the pixel value) pichX = height of the picture picwgoalX = desired width of the picture in twips

So, to insert a picture, you just need to open your picture, convert the data to hex, load these data into a string and add the RTF codes around it to define a RTF picture. Now, you have a self contained string with picture data which you can insert in the RTF code of a document. Don't forget the closing "}"

Next, you get the RTF code from your RichTextBox (rtbBox.Rtf), insert the picture at the proper location, and set the code of rtbBox.Rtf

One issue you may run into is that .NET RTB does not have a very good support of the RTF standard.

I have just made a small application* which allows you to quickly test some RTF code inside a RTB and see how it handles it. You can download it here: RTB tester (http://your-translations.com/toys).

You can paste some RTF content (from Word, for instance) into the left RTF box and click on the "Show RTF codes" to display the RTF codes in the right RTF box, or you can paste RTF code in the right RTB and click on "Apply RTF codes" to see the results on the left hand side.

You can of course edit the codes as you like, which makes it quite convenient for testing whether or not the RichTextBox supports the commands you need, or learn how to use the RTF control words.

You can download a full specification for RTF online.


NB It's just a little thing I slapped together in 5 minutes, so I didn't implement file open or save, drag and drop, or other civilized stuff.

Sunday, June 6, 2021
 
Stefan
answered 6 Months ago
22

The string "data:image/jpeg;base64," is not a valid base64 Encoded string. So it must be removed before decoding.

here you go.

String encodedDataString = "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBxQSEhUUEhQUFRQVFBQUFBQXFxQWGBQWFxUWFhQXFRUYHSggGBwlHBQUITEhJSkrLi4uFx8zODMsNygtLisBCgoKDg0OGxAQGywmHyQsLCwsLC4sLywsLCwsLCwsLCwsLCwvLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsLP/AABEIAJcBTgMBIgACEQEDEQH/xAAbAAACAwEBAQAAAAAAAAAAAAACAwABBAUGB//EAEUQAAEDAgIGBwQHBwMDBQAAAAEAAhEDIRIxBEFRcYGRBSJhobHB0RMykvAGFBVCUuHxU1RicoKT0jOiskNE4hYjNGNz/8QAGwEAAwEBAQEBAAAAAAAAAAAAAAECAwQFBgf/xAAwEQACAgECBAIKAwEBAQAAAAAAAQIRAxIhBDFBURNhBRQiQoGRobHh8FJx0TLxFf/aAAwDAQACEQMRAD8A5+FSEzArwL64+A3FwpCZgVhiA3FwpCbgUwJC3FwpCaGK8CLDcVCmFO9mrDEWFMThUhPwK/ZosNLM8K8Kf7NTAixaWIwqQnezU9mixUxMKQm4FMKLEKhSE3AqLEBYuFITMKmFAWLhVCZhUhAWLhSEyFITDULh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E8UBDp945ZZAX1RwSGgCwjg4DuutFJs5Ejs/QK6iTclyFOJE533n/kFALe6eEfkmGodojtEeMFBjPPWDHj4p7cg3KdXOsHLY+O6QkEgn3eLZy4LQGkbd8h/eJVNJ1kHhHmhRHqMzwDrbGwkE9/qrZMQCYnVJ8E2uwawN+tZNI0BhjqjfICmUPJMuEk+rQVPo91T3jjjVkCdUrTVY5v3GxtsZ1XuI+bKKJSdM3W8bZna/EbtiNcyZ7k5+jEQSMIdfMTAOwW7goonzqyG6uhwYANhjM3sTfIWyCjpLRhLtc5QIyjWooopWVboChTEAmZdOvXrIAsq0mm5omSQMyDHFRRTNbjg7RVKqTmesMna9x+SoylItn88FFE9EdNiU5OVA0asOLX5tG+PmFppFjx94do1bxN1FFCd8zSq5F1NGc28iM5vw7U2iXRMjfH5BRRPkhtWxrtKc3XLdZMEciJ8UTaw2luuBrv2BRRDlSsWncA6SJPXJ7CDvPZlKaykHZkZTlGedwJ2KlFljyOU9LLnjUY2hYols4nHLqkajkMQvbdtWY6Q7FDyPGRE9iiic8sovYUcMJLcZ9otgADxg8DKSdIbJJbBGy3gYUUXRBKa3RzSuL2YWCm6Ze5picp8BbmhGhBvWa8kDO0W4lUoo0xtqkW5SpOw6DpdYujDYSL75hGKjri1pizYEbZ17lFEpwUY2hQm5yplMc8yScvvdu5NNT8WWTjDe6ZKiiyeWVpdzTwY02W4tuW3i8QPGAVkr1YiYzibWOcSROsc1FEeJJNrsLw4tJ9x7W4RiIBBiD6/oipgOHVE9hy3QVFFnHPKUU3XOi5YYxk0m+QP1IOyJYRkAQQdsCAAgcI90hx2EEH0/VRRdsYI4XNidI0gs6tSROrqlU2hOQvnqHmFaizvematVG0TA7WTznxCH2rmmDHL9VFFrbVkJJoa58nqkneBBjO1kgVNQwk3tBEc1FERbYaVuEHuv1Z7QR3zlwSTWvBaZGwjzUUVW7qwik1yP/9k="

encdoedDataString = encodedDataString.replace("data:image/jpeg;base64,","");

byte[] imageAsBytes = Base64.decode(encodedDataString.getBytes(), 0);
imgView.setImageBitmap(BitmapFactory.decodeByteArray(
                imageAsBytes, 0, imageAsBytes.length));
Tuesday, September 21, 2021
 
tim_d
answered 3 Months ago
16

Use Html.fromHtml combined with your own implementation of Html.ImageGetter.

See here.

When overriding Html.ImageGetter.getDrawable, convert the Base64 string into a byte array (you can use android.util.Base64) and feed it into BitmapFactory.decodeByteArray to produce a Bitmap which you can then pass into the constructor of a BitmapDrawable to return.

For example:

Html.fromHtml(content, new Html.ImageGetter() {
        @Override
        public Drawable getDrawable(String source) {
            byte[] data = Base64.decode(source, Base64.DEFAULT);
            Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);                
            return new BitmapDrawable(getResources(), bitmap);
        }
}, null);
Thursday, November 11, 2021
 
icehawk
answered 3 Weeks ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share