Retrieving CSV file from Email Android 11

With Android 10+ Going forward will we be allowed to read CSV or text files from the Downloads folder? I do not need to be able to write to the downloads folder only read. If so I should be able to download a file from an email into Downloads, and then read it via my app and re-save to the ASD correct?

Is there an easier way to get a file from email that I am missing?

Thanks,
Jonathan

Are you using gmail ?

A quick howto:

Import a csv file from GMail

Preliminaries:
On your PC, login to your receiving google account
Open Gmail
Create a new google sheet
The scripting that follows was designed to run once a day,
and overwrite the existing data!

Part 1 - GMail Setup

In GMail, go to Settings, then Filters
Create a new filter
You want your filter to look for emails from a specific email address, with a specific subject, and that have an attachment:

Matches: from:(zzz@gmail.com) subject:(My CSV Data File) has:attachment

Then you want to set the actions:

Do this: Star it, Apply label "MYCSVFILE"

Part 2 - Google Sheet and Script

In your new google sheet, give the sheet a useful name
From Tools, open the Script Editor
A new script will open, give it a useful name
Add the following code:

function importCSVFromGmail() {


//gets first (latest) message with set label
var threads = GmailApp.getUserLabelByName('MYCSVFILE').getThreads(0,1);
var message = threads[0].getMessages()[0];
var attachment = message.getAttachments()[0];


 // is the attachment a suitable CSV file 
if (attachment.getContentType() === "text/csv") {                           
var ss = SpreadsheetApp.getActive();
var sh = ss.getSheetByName("Sheet1");
// remember to clear the content of the sheet before importing new data
sh.clearContents().clearFormats();                                        


//parses content of csv to array   
var csvData = Utilities.parseCsv(attachment.getDataAsString(), ",");      

//pastes array to sheet   
sh.getRange(1, 1, csvData.length, csvData[0].length).setValues(csvData);       
// mark message as read
message.markRead();                                                         
// nnstar the message
message.unstar();                                                           
  
confirmSuccess()
 
 } else {
confirmFail(); 
}
}

//sends email to owner after all other script elements have completed  //”assumes” success ;)
function confirmSuccess() {
MailApp.sendEmail("zzz@gmail.com", "CSV FILE IMPORT", "Script to create new listing completed successfully. This script runs every day between midnight and 1am");
}

//sends email to owner after all other script elements have completed  //”assumes” fail;)
function confirmFail() {
MailApp.sendEmail("zzz@gmail.com", "CSV FILE IMPORT FAILED", "Script to create new listing failed to complete. Please contact the google admin. This script runs every day between midnight and 1am");
}

Set up a time driven trigger for the script
That should be it

You can then download the data to your app.

(Credits Amit Agarwal for original idea)

I am using Gmail. It looks like you have a very nice script there, however, the last part is what I was trying to ask about. The downloading data to my app. That's the key part I am missing. How do I take an attachment from an email and get it into my app.

Looking more closely at your script: Correct me if I am wrong, but what you are doing is looking through your inbox for an email, if it has the proper attachment, save the file in Google Drive via Google sheets, then you would have your app download the file from Google Drive?

If that is the process, it would work but not what I am hoping for. It would be nice to be able to read the file from the download folder and save it into the ASD.

Thanks for your response,

Jonathan

Nearly....

In the script the data inside the csv file is applied to a google sheet.

You can then fetch that data from the google sheet using the web component:

e.g.

or

thank you for bringing up that question...

as far as I know, this is still an open question... what happens after App Inventor targets SDK 30?
Will we still be able to access the shared storage or will we be restricted to only the assets and ASD - application specific directory?

see also Overview of shared storage  |  Android Developers

Use shared storage for user data that can or should be accessible to other apps and saved even if the user uninstalls your app.

@ewpatton it would be great to get your answer on this important topic...

Taifun


Trying to push the limits! Snippets, Tutorials and Extensions from Pura Vida Apps by icon24 Taifun.

You got my question Taifun, thanks!