android: how to open a text file using execsys

Other discussions about CSPro
Post Reply
bulakenya
Posts: 3
Joined: March 25th, 2014, 4:38 am

android: how to open a text file using execsys

Post by bulakenya »

hello...

need help, how can i open a text file using execsys(), my text file is located at csentry folder in my tablet?

thank you...
josh
Posts: 2399
Joined: May 5th, 2014, 12:49 pm
Location: Washington DC

Re: android: how to open a text file using execsys

Post by josh »

On Android you can use execsystem("view:<pathtotextfile>"). If your text file is named myfile.txt in the csentry directory then you could do:

execsystem("view:/sdcard/csentry/myfile.txt")

you can also place text files in your application directory (the folder where your .pen file is) and then use the pathname() function to get the path to them:

execsystem(maketext("view:%smyfile.txt", pathname(Application))

Depending on what app is set to view text files on your Android device you may or may not have trouble with formatting. On a couple of devices I have tried the default text viewer does not used a fixed width font which makes it hard to format the text nicely. If that is a problem you may want to look at using HTML instead of text.

Here is an example function that writes out a text file to the application directory and displays a simple text file (works on both Android and Windows):
// Write out and display household summary report
function showHouseholdReport()
    
string reportFilename = maketext("%sreport.txt", pathname(Application));
    
setfile(tempFile, reportFilename);
    
filewrite(tempFile, "Household Summary Report");
    
filewrite(tempFile, "------------------------");
    
filewrite(tempFile, "");
    
filewrite(tempFile, "District %d Village %d Household Number %d",
                        
visualvalue(DISTRICT),
                        
visualvalue(VILLAGE),
                        
visualvalue(HOUSEHOLD_NUMBER));
    
filewrite(tempFile, "");
    
filewrite(tempFile, "Members 5 years and over:");
    
filewrite(tempFile, "");
    
numeric i;
    
filewrite(tempFile, "Name                           Sex    Age  Relationship");
    
filewrite(tempFile, "----                           ---    ---  ------------");
    
do i = 1 while i < totocc(ADULTS000)
        
filewrite(tempFile, "%s %-6s %3d  %s",
                            NAME(i),
                            
getlabel(SEX, visualvalue(SEX(i))),
                            
visualvalue(AGE(i)),
                            
getlabel(RELATIONSHIP_VS1, visualvalue(RELATIONSHIP(i))));
    
enddo;  
    
close(tempFile);
    
if getos() in 20:29 then
        
// Android - use "view:"
        execsystem(maketext("view:%s", reportFilename));
    
else
        
// Windows - use "explorer.exe <filename>"
        execsystem(maketext("explorer.exe %s", reportFilename));
    
endif;  
end;
Here is a similar example but using HTML:
// Write out and display household summary report using HTML
function showHouseholdReportHTML()
    
string reportFilename = maketext("%sreport.html", pathname(Application));
    
setfile(tempFile, reportFilename);
    
filewrite(tempFile, "<!DOCTYPE html>");
    
filewrite(tempFile, "<html><head>");
    
filewrite(tempFile, "<style type='text/css'>");
    
filewrite(tempFile, "table, th, td {border: 1px solid black;border-collapse: collapse;padding: 8px}");  
    
filewrite(tempFile, "</style>");
    
filewrite(tempFile, "<title>Household Summary Report</title>");
    
filewrite(tempFile, "</head>");
    
filewrite(tempFile, "<body>");
    
    
filewrite(tempFile, "<h2>Household Summary Report</h2>");
    
filewrite(tempFile, "<p>District: %d</p>",
                        
visualvalue(DISTRICT));
    
filewrite(tempFile, "<p>Village: %d</p>",
                        
visualvalue(VILLAGE));
    
filewrite(tempFile, "<p>Household Number %d</p>",
                        
visualvalue(HOUSEHOLD_NUMBER));
    
filewrite(tempFile, '<img src="photos/photo-%d-%02d-%03d.jpg" alt="household photo" width="300">',
              
visualvalue(DISTRICT), visualvalue(VILLAGE),
              
visualvalue(HOUSEHOLD_NUMBER));
    
filewrite(tempFile, "<p>Members 5 years and over:</p>");
    
numeric i;
    
filewrite(tempFile, "<table>");
    
filewrite(tempFile, "<tr><th>Name</th><th>Sex</th><th>Age</th><th>Relationship</th></tr>");
    
do i = 1 while i < totocc(ADULTS000)
        
filewrite(tempFile, "<tr>");
        
filewrite(tempFile, "<td>%s</td>", NAME(i));
        
filewrite(tempFile, "<td>%s</td>",
                  
getlabel(SEX, visualvalue(SEX(i))));
        
filewrite(tempFile, "<td>%d</td>", visualvalue(AGE(i)));
        
filewrite(tempFile, "<td>%s</td>",
                  
getlabel(RELATIONSHIP_VS1,
                  
visualvalue(RELATIONSHIP(i))));
        
filewrite(tempFile, "</tr>");
    
enddo;  
    
filewrite(tempFile, "</table>");
    
filewrite(tempFile, "</body></html>");
    
close(tempFile);
    
if getos() in 20:29 then
        
// Android - use "view:"
        execsystem(maketext("view:%s", reportFilename));
    
else
        
// Windows - use "explorer.exe <filename>"
        execsystem(maketext("%sexplorer.exe %s",
                            
pathname(Windows),
                            reportFilename));
    
endif;
end;
sah
Posts: 97
Joined: May 28th, 2015, 3:16 pm

CSPro Andriod & learning Materials

Post by sah »

Hello Martin & Josh
I just got introduce to your awesome software cspro and i have been learning new stuff. I have gone through previous post (04-Survey) and I would like to know if there are materials to the Android Capi programming.

On the other note, could you kindly advise which approach is better when creating the set thus having folders(Data, Entry, Dict) like the setup of mics and your Survey(all in one folder)

Attached is material of mics set up I have used to learn cspro.
On the other note, is it positive to share your skype googletalk address for online live chat for further consultation.
Attachments
MICS5_Data_Entry_Application_2014.09.18.zip
(1.07 MiB) Downloaded 397 times
htuser
Posts: 631
Joined: December 19th, 2011, 6:26 pm
Location: Silver Spring Area, MD, USA

Re: android: how to open a text file using execsys

Post by htuser »

@Josh, Genius! for the HTML report. Now i can report whole questionnaire and save them in pdf... It's a great example interacting Cspro with other programming language!
But, i would like to know it there's no way to save them natively to pdf? Maybe, the pdf is a proprietary format? Or a very difficult encoded format? Now, we can only save to pdf via the browser. Other, why using view on Android? Why you don't open it directly on Chrome navigator?
Thanks a lot for your precious help!
G.VOLNY, a CSProuser from Haiti, since 2004
bulakenya
Posts: 3
Joined: March 25th, 2014, 4:38 am

Re: android: how to open a text file using execsys

Post by bulakenya »

@sir josh, thank you very very much for your help it is very much appreciated... :-)
josh
Posts: 2399
Joined: May 5th, 2014, 12:49 pm
Location: Washington DC

Re: android: how to open a text file using execsys

Post by josh »

@htuser while PDF is an open format, it is a very complex one. Generating a PDF file requires a writing a binary file with compressed chunks in it which is not possible in CSPro.

The reason to use view rather than launching Chrome directly is that on different Android devices you may or may not have Chrome installed. Using view will launch whatever viewer is associated with html files so it will work no matter what web browser the user has installed.
Post Reply