Upload file to server hosting by PHP. Encode file to Base 64 extension

yes, installed in Android 10 or 11.

Tested apk Android 10 and 11. Works OK. (Couldn't see if the directory intoASD was created in Android 11 due to Google lockdown, but created in Android 10, but no errors).

I guess we just need an if else block if we are working in development mode to access assets, in the MIT companion directory.

Thanks @TIMAI2 for testing it on Android10-11.

With double slash //gato.png works in development mode and installed.

- Image in asset to Base 64 and upload to web.

p327C_subir_archivo_imagen_Base64_ASD.aia (1.7 MB)

  • This example:

  • Converts an asset image to a Base 64 string, with block "FileToStringASD"

  • Show that string in a Label.

  • Convert that string to an image called: "/this_is_a_cat.png", save it in ASD, and show it in Image2

  • With block "PostText", sends string to web.

  • A PHP file converts that string to image: dibujo.png in web server.

  • You can look that image in a web page.
    (This web page always shows the image in a size of 40x40)

PHP code:

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

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

$plainText = base64_decode(str_replace(array('-', '_',' ','\n'), array('+', '/','+',' '), $contenido));
$ifp = fopen( "dibujo.png", "wb" );
fwrite( $ifp,  $plainText );
fclose( $ifp );

echo "Creo que ha subido";
?>

- PICK Image and convert to Base 64, upload to web. PHP.

p327C_subir_archivo_imagen_Base64_ASD_PICK.aia (1.7 MB)

  • We can explorer and PICK an image file with ActivityStarter:
    ActivityStarter1.Action = android.intent.action.PICK
    ActivityStarter1.DataType = image/*
  • We get ResultUri, example:
    content://com.mi.android.globalFileexplorer.myprovider/external_files/gato.png
  • Using the UriToPath block, we convert that uri to the path of the file:
    /storage/emulated/0/gato.png
  • We remove the base of that address to obtain /gato.png
  • That file to string: FileToString block.
  • That string to file in ASD: StringToFileASD.
  • With PostText upload string to web.
  • PHP file receives the string and converts it to the file dibujo.png
  • Show that image in a web page.

- PICK Image. Copy in ASD. Convert to Base 64. Upload to web. PHP.

  • Can anyone check if this app works installed on Android 11? Thanks.
    (use images smaller than 500 kB)

p327C_subir_archivo_imagen_Base64_ASD_PICK_CopyASD.aia (1.7 MB)

  • This app:

  • PICK an image. Convert Uri to Path.

  • Copy image to ASD with name: /image_in_ASD.png

  • Convert image ASD to string Base 64.

  • Upload string to webserver with PHP by Web component.

  • Show image in a web page in my hosting.

Hi Juan

Tested compiled on Android 11.

Picks image OK.

Doesn't show image2
Doesn't show web image

Thanks @TIMAI2

- Download a video file to the ASD, convert it to a Base 64 string, and then convert the string to a new video file.

p327_extensionbase64_bajarvideo.aia (22.4 KB)

  • On this website we can see samples of video files:
    https://filesamples.com/formats/mp4

  • Let's download the file: sample_960x400_ocean_with_audio.mp4 (16,7 MB)

  • We are going to follow this process:

1.- We download the video file to the ASD.
2.- We obtain the absolute address and put it in the source of the VideoPlayer.
3.- Start VideoPlayer.
4.- Convert file to string Base 64.
5.- Show first 100 chars.
6. Convert string Base 64 to file sample_mirror.mp4
7.- Start VideoPlayer with sample_mirror.mp4

Hello Juan,
I meet a problem to get the good path to find the file.
The code under used to work previously but it seems that with the ASD the path changed.
Can you please advise me.
Thanks.

Try with App Inventor.
Does the example in the previous post work for you?
What version of Android are you using?

I am using Android 11. Your exemple works.

I am just trying to encode a file in the DCIM folder (image taken with my camera).

I don't succed to have the good path.

Are you using App Inventor ? (or Kodular). I ask because Kodular appears (to me) to handle the file paths in Android 11 differently.

I am using Kodular

FileToStringASD has as base directory: /storage/emulated/0/Android/data/'namepackage'/files

FileToString block, base directory is:
/mnt/sdcard

Try FileToString with fileName:
/DCIM/Camera/yourvideo.mp4

@TIMAI2
Is it possible to copy, with known extensions, a file from DCIM to ASD in Android 11?

I don't have an immediate answer, I would have to build something and test. I may not be the best person to ask, but I know who is...@Anke :smiley:

The DCIM folder should be readable from an app on Android 11....

I asked about Kodular because when using Kodular creator, the image picker doesn't behave in the same way as the one on App Inventor (which, in my experience, creates a copy of the image selected in the ASD, whilst Kodular does not)

Yes, even when AI2 / Kodular targets Android 11 (targetSdkVersion = 30 → August 2021).
(Of course you need READ permission for this.)

Yes, because you do not need WRITE permission to write / copy to ASD.

Note: WRITE_EXTERNAL_STORAGE permission is no longer available for targetSdkVersion = 30 (Aug 2021), so you cannot save / copy to the external storage (except for the ASD). READ_EXTERNAL_STORAGE permission behaves the same way under Android 11 / (targetSdkVersion = 30) as before.

@Anke, does the example in post 19 work for you?
I have added the READ permission.
I have updated it here:
p327C_PICK_ASD_AI2.aia (22.4 KB)

It's about the following:

  • PICK an image from DCIM. Convert Uri to Path.
  • Copy that image to ASD with name: /image_in_ASD.png
  • Convert image ASD to string Base 64.
  • Upload string to webserver with PHP by Web component.
  • Show image in a web page in my hosting.
1 Like

Hi Juan, that last one works just fine for me (finally :wink: ), both companion and compiled on Android 11 (Google Pixel 4a)

1 Like

LGTM, seems to work on Android 9 and 11 (Companion).
Android 11 with APK: OK
:+1:

1 Like