Extension: File to String. String to File. Base64.

4,769 views
Skip to first unread message
Assigned to juanan...@gmail.com by carte...@gmail.com

Juan Antonio

unread,
Aug 30, 2017, 8:06:53 AM8/30/17
to MIT App Inventor Forum
Hi friends,

this extension convert image, sound, doc,... file to String in Base64, and String Base64 in file.

Usefulness: 

- convert a image, sound, doc,.. to String Base64 and then save this String in TinyDB. Then recovery String B64 from TinyDB and convert to File.

- convert a file to String and send this String by Bluetooth.





p177i_Extension_File2String.aia
com.KIO4_File2String.aix

MiuMiu

unread,
Aug 30, 2017, 11:56:49 AM8/30/17
to MIT App Inventor Forum
great!very useful extension!than you!

Abraham Getzler

unread,
Aug 30, 2017, 2:05:23 PM8/30/17
to MIT App Inventor Forum

TimAI2

unread,
Mar 4, 2018, 10:25:43 AM3/4/18
to MIT App Inventor Forum
Note: needs full path if using files in assets e.g.

when in development
/mnt/sdcard/AppInventor/assets/

when in production

/android_asset/


TimAI2

unread,
Mar 4, 2018, 6:47:14 PM3/4/18
to MIT App Inventor Forum
Also a problem when trying to decode in my Google web app.

If I encode an image to base64 using bash the output works, if I encode using the extension I get a different output and the webapp cannot decode the base64 string

From Ai2

iVBORw0KGgoAAAANSUhEUgAAAA4AAABnCAYAAAAws3NDAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz
AAAHXgAAB14BODEn8gAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAAAAwSURB
VFiF7cuxDQAACAMg9f-f6w8mbrDTSVIHc0miKIqiKIqiKIqiKIqiKIqi-BIXrUIEyqlMIQQAAAAA
SUVORK5CYII

From bash

iVBORw0KGgoAAAANSUhEUgAAAA4AAABnCAYAAAAws3NDAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz
AAAHXgAAB14BODEn8gAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAAAAwSURB
VFiF7cuxDQAACAMg9f+f6w8mbrDTSVIHc0miKIqiKIqiKIqiKIqiKIqi+BIXrUIEyqlMIQQAAAAA
SUVORK5CYII=

You will see that the encodes are slightly different (+'s and -'s, and no = at the end) 


Code in web app:

var data = Utilities.base64Decode(e.parameters.file, Utilities.Charset.UTF_8);


 

Juan Antonio

unread,
Mar 5, 2018, 8:49:09 AM3/5/18
to MIT App Inventor Forum
Hi Tim, the idea of this extension is get an external file, perhaps with an Explorer, and upload to web o save to TinyDB, or other file.



This extension use Web Safe, change +-=  so they do not have a problem when traveling on the internet.
When they arrive at the server, change those characters again.

<?php
// Juan A. Villalpando
// kio4.com

$datos=$_POST;
$contenido=$datos['contenido'];

$archivo = 'imagen.txt';
$auxi = fopen($archivo, 'w');
fwrite($auxi, $contenido); 
fclose($auxi);

echo $contenido;

$foto = 'foto.png';
$auxi = fopen($foto, 'w');
$contenido2 = strtr($contenido, '-_,', '+/=');
$contenido2 = base64_decode($contenido2);
fwrite($auxi, $contenido2); 
fclose($auxi);

?>
http://kio4.com/appinventor/277_extension_imagen_string.htm
https://stackoverflow.com/questions/24460422/how-to-send-a-message-successfully-using-the-new-gmail-rest-api/24461102#24461102

TimAI2

unread,
Mar 5, 2018, 9:20:10 AM3/5/18
to mitappinv...@googlegroups.com
OK Juan, I understand, but thought you should know it doesn't work in some circumstances where the receiving end is not expecting "Web Safe"!

If I get a chance, I will add some find/replace code to my web app and try your extension again :)

I found ghostfox's similar extension on thunkable that does work so no worries :)

Juan Antonio

unread,
Mar 6, 2018, 4:21:59 PM3/6/18
to MIT App Inventor Forum
Hi Tim, 

I am testing this new version of the Base64 extension.
In this example the extension gets a file from the Assets, converts it to StringBase64 and presents it in Label1.
Then take that text and save it in a file.
Then read that file and present it in Label2.
Can you try it please?


I think the conversion to Base64 is different from others, but it works.

Regards.

TimAI2

unread,
Mar 6, 2018, 5:26:33 PM3/6/18
to MIT App Inventor Forum
Hi Juan

Yes, I can see this works inside AI2. I tested my image on it as well (the one that generated the base64 above) and your new version appears to encode it in the same way - with +'s and ='s  :)

If you wish, private email me the aix file and I will test out on my google drive upload example app.

Regards

Tim
Reply all
Reply to author
Forward
0 new messages