{"id":5429,"date":"2024-12-28T14:43:21","date_gmt":"2024-12-28T14:43:21","guid":{"rendered":"https:\/\/obsidiansofteducation.com\/?page_id=5429"},"modified":"2024-12-28T14:57:07","modified_gmt":"2024-12-28T14:57:07","slug":"upload-any-file-to-google-drive-in-mit-app-inventor-part-2","status":"publish","type":"page","link":"https:\/\/obsidiansofteducation.com\/index.php\/upload-any-file-to-google-drive-in-mit-app-inventor-part-2\/","title":{"rendered":"Upload ANY File to Google Drive in MIT App Inventor (Part 2)"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Hello friends and welcome to Obsidian Soft,<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/obsidiansofteducation.com\/wp-content\/uploads\/2024\/12\/29.png\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">In my\u00a0<a href=\"https:\/\/obsidiansofteducation.com\/index.php\/upload-any-file-to-google-drive-in-mit-app-inventor-part-1\/\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>last tutorial<\/strong><\/a>, I made a full app that took a picture using the camera component of MIT App Inventor and uploaded this pic to a folder in Google Drive as many of my viewers had requested for this tutorial.\u00a0<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Today, I will show you how to make our code so generic that we are effectively able to upload ANY type of file to Google Drive be it text, audio, video, or image.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This tutorial assumes that you have completed the\u00a0<a href=\"https:\/\/obsidiansofteducation.com\/index.php\/upload-any-file-to-google-drive-in-mit-app-inventor-part-1\/\"><strong>picture uploading app i.e. part 1 of this tutorial<\/strong><\/a>\u00a0as we wrote some essential script in it and we also made a public folder in Google Drive.\u00a0<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So, let\u2019s begin\u2026<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Open up your&nbsp;<strong>UploadFileToGDrive<\/strong>&nbsp;project in MIT App Inventor and go to the blocks section.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">First of all, let\u2019s make a procedure for uploading files that takes in two inputs. Use the first procedure type from Procedures (the one that doesn\u2019t return a result) and click on the cog wheel to add two inputs. Rename inputs to&nbsp;<strong>fileExt<\/strong>&nbsp;and&nbsp;<strong>filePath.<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/obsidiansofteducation.com\/wp-content\/uploads\/2024\/12\/JURddpnLH6CFDiOwN2-hhY2KxLQRZWaQdgEsQS6PK03ihB1MzthqYGLi-9moeLDI-ui8RPYPfJ8Sg4og4hmooPK147pXui6hQEXTVIWmEXcHK-V86auVn0HhyFyvYKJOjEfw3a5tpKErGOAo8uGk5sQ.png\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Now, let\u2019s look at our uploadPicBtn code and try to make it more generic.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Look at this part:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/obsidiansofteducation.com\/wp-content\/uploads\/2024\/12\/3sLtBf05lDjd0UfZCFnjA-q9nO0nEcPw1VjsHWmk2DiO8RR_6FoJcDtrgSkMz5c1Ca_9F0tyR-RqGOGwaKbToKzCJLYKQOZKi0A4MUrRVxGmliHPmbLc3dU0ZHEnJPQ18AOMGe1yAq6vtdrwQ_H4h-8.png\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">We are doing a special thing here by removing \u201cfile:\/\/\u201d from the path of the saved camera image so how about we remove the correction of the image path from here and take it to the Camera\u2019s AfterPicture event.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So, update your After Picture event to this:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/obsidiansofteducation.com\/wp-content\/uploads\/2024\/12\/qOGBoQ-Kb0SoMXfAnqWDuCmmxVFCw9eFsyktHGTsF-f48O6ao7dfcMRB2miWNt3AAX7oG93g-9YPlSo4lWVQCGgJHPfRLvKeoe2m652jpwzH_B_FAJWr9ySJNKa935cmqMqw9m6cAGu-fq36erkTxU8.png\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">This way we correct the path by removing file:\/\/ before assigning it to imageToUpload so it is correct from the very beginning.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Hence, we can directly give imageToUpload here in our uploadPicBtn Click event.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/obsidiansofteducation.com\/wp-content\/uploads\/2024\/12\/0nZgdyshDdtK4MOw4-7md7mZJCbXzqIA_B7COjpBzRkwRpTUe5n7GbAkHeu4L2SqDYxjsK9ZDK1P-BhO0b3DCXBYjI9b7P37dT1PejxlsB50FoDsFmHEYqA0AgN5tVp3tR8IAo0wa9R25RIhPs7j_TI.png\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">So, this is more or less generic now and we can move the entire uploadPicBtn code to our custom procedure uploadFile. Next, we modify it to remove all image-related parts.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/obsidiansofteducation.com\/wp-content\/uploads\/2024\/12\/q7Gx8G0gncqj4024A1DXtCpeLImfBRUb-qlx3x-vONzHCv4daGVMUNESYPkYMJT-1lloNCL2OjshlpIQRrZ1Tr3WNt3D98Efocedk9X7xJn_yi38E7oWbeWmDliJAn9sdnOmt_HzTsUo_Lt0sUrFjpU.png\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Our final uploadFile procedure looks like this:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/obsidiansofteducation.com\/wp-content\/uploads\/2024\/12\/xNOIkhFTawMQOFrhRTeVNxbpJrdeN41E9FloyzyOjTH7TH5oYBmGCzDo3yCIDJPv12ufZIMqnZHkRzgYbQS9FlfQ6-XuGSJDRpui2IJc2nCP-rpv4CjDbi82CpOj9O3GKrUl-S4NRDTJGQ_wsn_14H0.png\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">But what about our uploadPicBtn code as it is empty now.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We have to call the procedure uploadFile and give it image-related information as input.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/obsidiansofteducation.com\/wp-content\/uploads\/2024\/12\/xBIX1TSJa0pXMhzZPgJuAAHvvT1aolEkqwrislnGpRd-NqCX2jXmChVYMuZNUw1AExfE7NEBHSmJvHrOAwnkwaR3CoT7K2itRllrBT8wzqIm2nnj9Tm83EeJCsNzjZUI-1FieU_lDszltftyT-KL8JAw640-h188.png\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">So, we can use this uploadFile procedure to upload any file as long as we know its file path.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So, let\u2019s add some more components to our screen for uploading text and sound files by going to Designer View.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Drag and drop another horizontal arrangement from&nbsp;<strong>Layout<\/strong>&nbsp;below the camera buttons one. Make the width: fill parent, height: 13% and align horizontal and align vertical both center.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Drag and drop a&nbsp;<strong>textbox&nbsp;<\/strong>from the&nbsp;<strong>User Interface<\/strong>&nbsp;palette inside this second horizontal arrangement. In its properties, make it multiline. Drag and drop a button next to this textbox and rename it to saveTextFileBtn and change the text on it in its properties to \u201cSave\u201d.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Drag and drop another button next to the save button and rename it to uploadTextFileBtn and change the text to \u201cUpload File\u201d.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Select the second horizontal arrangement and duplicate it by pressing&nbsp;<strong>Ctrl + C and Ctrl + V<\/strong>&nbsp;on the keyboard for&nbsp;<strong>Windows<\/strong>&nbsp;OR&nbsp;<strong>Command + C and Command + V<\/strong>&nbsp;on the keyboard for&nbsp;<strong>MacOS.<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Delete the text box from this arrangement. Rename the saveTextFileBtn2 to startRecBtn and change text in its properties to \u201cStart Rec\u201d. I am going to use the second button for two things: stopping the recording and uploading it simultaneously but you can have two buttons for this work!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So, rename it to stopRecUploadBtn and change the text in its properties to \u201cStop Rec &amp; Upload\u201d.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now, drag a<strong>&nbsp;Sound Recorder<\/strong>&nbsp;component from the&nbsp;<strong>Media<\/strong>&nbsp;palette onto the viewer.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We also need a&nbsp;<strong>File&nbsp;<\/strong>component so get it from the&nbsp;<strong>Storage<\/strong>&nbsp;palette.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We have added all that we need for this project in the designer section so let\u2019s go back to the blocks section.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When saveTextFileBtn is clicked, we want to save whatever is inside the Textbox as a text file.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So, use File\u2019s saveFile procedure to do this. We need to provide a fileName but don\u2019t be confused with our global fileName variable as that is for uploading the file with a unique name to Google Drive. The filename required by SaveFile procedure is for saving the file on your device. So, give some name prefixed by \/ to make sure that the file is shareable. If you don\u2019t prefix it by \/, it will be considered a private file and we will not be able to get the path for it.&nbsp;<strong>Also, do provide it a file extension i.e. \u201c.txt\u201d<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/obsidiansofteducation.com\/wp-content\/uploads\/2024\/12\/eNdblx5YS48fdxSfP4hANnjlFdQJU-y2fxCE3sye6DvNn2MO-VAvbuwCmBTvbavatC1X7swtY4mnx-6JT2iYBjtsujcspZe2i81_D0eMme0fzIYURjrYjcKPYxqGmOE4NwJELK4mrTCdE6-iMg3jE98.png\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The File component\u2019s SaveFile procedure on successful completion, triggers the AfterFileSaved event that returns a fileName but we have a problem as this fileName contains just the file name that we provided when we pressed the save text file button and it doesn\u2019t give us the path. Remember that our uploadFile procedure requires an absolute file path for any file that we want to upload to Google Drive.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/obsidiansofteducation.com\/wp-content\/uploads\/2024\/12\/PArsTOEUW7Drcj-WNILY-piQeXg56Pa1RNxy8WYQuB06shSl4tdNyMjur_7yJo6-BrbugbKeqnCXadhohN8Q9MYPJmi6tEVQCDOl79B7Mj4HTL4hfjIJkbzT8SoGdrJ7JDc1hoicVqrb1YDUSs_4P1A.png\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Fortunately, there is a procedure in our downloaded extension for getting the App-specific directory\u2019s path where our text file was saved.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So, add the following code to AfterFileSaved event for setting the textFileToUpload (text file\u2019s absolute path) to a join of app specific directory\u2019s path and the fileName.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/obsidiansofteducation.com\/wp-content\/uploads\/2024\/12\/5WTxg4-fzKevk48WgnI8wCFwgD09TY8MNcK93c8m409BghhVnJTPOxaHUD7Qe68DBcodShpBQ55_cCv-tp_E534yVqk3_UX0XYs6S9DJgWH94SCNXLVrA72qbHh2FGyjHOFql2p-9YuyEa_5QF2eF_U.png\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The uploadTextFileBtn code is the similar to uploadPicBtn code.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/obsidiansofteducation.com\/wp-content\/uploads\/2024\/12\/ajJvzcBkI5l_oF_QmE2DtUU_2kaMxXM33km3fuBjlQdl38w2yISdKRw3CYBuPQPrbxEhSy8eSN5s8roJGihlP7jbDzooLvppLs1R3jRgQUfGl4Y9a8stgsDXNLfGRNFt14D62DFQgdyYYs-8lA70Rr0.png\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Last is our sound recording and uploading part.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When startRecBtn is clicked, call soundRecorder to start.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/obsidiansofteducation.com\/wp-content\/uploads\/2024\/12\/2y0a1UkMkTpQioDkHQA2okTIxCaMAJ6QxgSaEUePlwy9PSyAf_dV3A3jb-vjvm8aTV3AEBmHEJ74AJzTsvYxWtejdupmpsUumhJpbbIulpd8f7Z6Df3ghf8PGIC8_fJUmble4aIAeDVhlpp0hG6kwIo.png\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">When the stop and upload button is clicked we will stop the sound recorder.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/obsidiansofteducation.com\/wp-content\/uploads\/2024\/12\/ZS6t2xASQxt2h4tPXBsnj59gkfEiMxEBcO_bkwG2PVzOafZHx2InFWvwFnQHNO8kPBi04Bm7B2vX79HaVQB6kduGASQ4qFIG4u2Gb0_RodOY0h9qnIT2Va9LlFoy3a4zQ_Ax4hjfYld6YVG184VY-jY.png\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The stopping of the sound recorder will trigger the AfterSoundRecorded event. This will give us a sound variable which is actually the correct path of the recorded sound so we can use it safely without any changes in our uploadFile procedure.&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/obsidiansofteducation.com\/wp-content\/uploads\/2024\/12\/fDZN6VaoP7pbb7Py3g5uSAznVoF7x5KM7op9Mfe9qaoJHceCbBYRzxx8gDnuuSeyP1_IcvBsY9Vgp34mI5Do25p3zbFQgB9T5k1yVuWdv10CAzZDtt2AugcE5SM-6eMBWIczDCBukJOMFfkPfaw5sa0.png\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">And we are done.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So, we have a custom procedure in this app that can upload a file created in our app to a public folder in our Google Drive.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I hope you liked this tutorial.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you like my tutorials, consider supporting me:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter is-resized\"><a href=\"https:\/\/obsidiansoft.gumroad.com\/l\/cxdft\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"372\" height=\"196\" src=\"https:\/\/obsidiansofteducation.com\/wp-content\/uploads\/2024\/12\/donate-button-1.png\" alt=\"\" class=\"wp-image-5381\" style=\"width:234px;height:auto\" srcset=\"https:\/\/obsidiansofteducation.com\/wp-content\/uploads\/2024\/12\/donate-button-1.png 372w, https:\/\/obsidiansofteducation.com\/wp-content\/uploads\/2024\/12\/donate-button-1-300x158.png 300w\" sizes=\"auto, (max-width: 372px) 100vw, 372px\" \/><\/a><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/www.buymeacoffee.com\/obsidiansoft\" target=\"_blank\" rel=\"noopener\"><\/a>You can also watch the video tutorial for this part on my channel: Obsidian Soft.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<p class=\"responsive-video-wrap clr\"><iframe loading=\"lazy\" title=\"Upload Sound to Google Drive App Inventor | Upload ANY File to Google Drive in App Inventor Part 2\" width=\"1200\" height=\"675\" src=\"https:\/\/www.youtube.com\/embed\/5gcGzOvYFXk?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<\/div><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Please like my videos and share them with your friends and family. Also, subscribe to my channel and press the bell icon so you don\u2019t miss any of the great projects I have planned for you.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/www.youtube.com\/c\/obsidiansofteducation\" target=\"_blank\" rel=\"noopener\">https:\/\/www.youtube.com\/c\/obsidiansofteducation<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Please like my social media pages for more educational resources and tips.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Facebook:<a href=\"https:\/\/www.blogger.com\/blog\/page\/edit\/4947000819484484884\/7140650632339107612#\" target=\"_blank\" rel=\"noopener\">&nbsp;https:\/\/www.facebook.com\/ObsidianSoft\/<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Instagram:<a href=\"https:\/\/www.blogger.com\/blog\/page\/edit\/4947000819484484884\/7140650632339107612#\" target=\"_blank\" rel=\"noopener\">&nbsp;https:\/\/www.instagram.com\/obsidiansoftapps\/<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Pinterest:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/www.pinterest.co.uk\/obsidiansoft\/_created\" target=\"_blank\" rel=\"noopener\">https:\/\/www.pinterest.co.uk\/obsidiansoft\/_created<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For links to free educational apps, have a look at the&nbsp;<a href=\"https:\/\/obsidiansofteducation.com\/index.php\/mobile-apps\/\">educational apps page<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello friends and welcome to Obsidian Soft, In my\u00a0last tutorial, I made a full app that took a picture using the camera component of MIT App Inventor and uploaded this pic to a folder in Google Drive as many of my viewers had requested for this tutorial.\u00a0 Today, I will show you how to make [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-5429","page","type-page","status-publish","hentry","entry"],"_links":{"self":[{"href":"https:\/\/obsidiansofteducation.com\/index.php\/wp-json\/wp\/v2\/pages\/5429","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/obsidiansofteducation.com\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/obsidiansofteducation.com\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/obsidiansofteducation.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/obsidiansofteducation.com\/index.php\/wp-json\/wp\/v2\/comments?post=5429"}],"version-history":[{"count":3,"href":"https:\/\/obsidiansofteducation.com\/index.php\/wp-json\/wp\/v2\/pages\/5429\/revisions"}],"predecessor-version":[{"id":5440,"href":"https:\/\/obsidiansofteducation.com\/index.php\/wp-json\/wp\/v2\/pages\/5429\/revisions\/5440"}],"wp:attachment":[{"href":"https:\/\/obsidiansofteducation.com\/index.php\/wp-json\/wp\/v2\/media?parent=5429"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}