Android Question Retaining tabular format when e-mailed

Cantaflex

Member
Dear Experts,

The screenshots below show the output from my recent training App and are fictitious results presented as labels in a TabHost panel on screen. Since I have control of the font used, the lists are neatly tabulated and meaningful, while the chart is a good summary of progress.

Screen457.png
Screen512.png


Screen544.png

My problem is that I also need these results to be sent to the trainee's supervisor. I have no control of fonts for e-mail, so the end result is not good. Simply inserting the label data again in the e-mail body is not the solution and nobody will be surprised at the mess produced, which I have copied below.

Screen435.png


Clearly there must be another way! However, I tried saving the labels as images to use as attachments to the e-Mail, but the 'Image' type doesn't seem to be recognised (maybe a library I don't have?). The chart is in a panel in a TabHost and I can't get at that either. I'm clearly missing something and would appreciate a push in the right direction. If I can solve this problem the App will be complete.

Very Best, David
 

JohnC

Expert
Licensed User
Longtime User
Well, two quick ideas come to mind:

1) Create a screenshot of your nicely formatted data and send the screenshot image as an attachment or inline image in the email:


2) Or, you will probably need to reformat the data in HTML table format and then send that HTML in the email.
 
Last edited:
Upvote 0

Cantaflex

Member
Many thanks JohnC - I'll pursue both of those ideas. If I can automate the attachments to the default e-Mail my problem should be solved. What great place this forum is!
 
Upvote 0

Cantaflex

Member
Many thanks again JohnC. Using your example and Erel's "takeScreenshot" subroutine I succeeded in creating a bitmap of the graph shown above and saving that as a JPEG file to the File.DirInternal directory for my App. Just a few lines of code. However, when trying to attach the .JPEG to an e-mail I find that GMail denies access to the DirInternal folder. It is only GMail that won't attach it - an older tablet will happily list the Chart.jpg file as an attachment, but in that case (even though the Google e-mail account is correctly specified) GMail will not allow that old e-mail app to 'sign in'. (Seems odd that Google denies access to its own Android directory while allowing older e-mail apps to extract files from it, but I'm too old to be surprised at anything now.)

Trying to copy the file to DirDefaultExternal or DirRootExternal fails with a "FileNotFoundException" error and PermissionDenied when trying to open the target, even after confirming write access with File.ExternalWritable. For the simple routine below to prove so error-prone I am beginning to doubt my logic!

Log("Copying file to external(emulated)")
File.Copy(File.DirInternal,"chart.jpg",File.DirRootExternal,"graph.jpg")
Log("Copied out")

I think the principle is right and offers a solution, but access to files should be simpler than this. Any thoughts would be very welcome.

Very Best, David
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
I think to fix this issue, you will first need to make your app be a "File Provider":

Then instead of specifying a file "directory" to gmail for the attachment, you will instead provide the "FileURI" of the file to Gmail:
 
Last edited:
Upvote 0

Cantaflex

Member
Thank you again JohnC. I'm learning more about B4A and Java tackling this problem than I would have thought possible in the last few days. One worry is I can't find anything about the FileProvider class in the documentation, although there is mention of FileUri(Dir as String,FilName as String)as String as a Member of the XUI Object. This rather begs the question - if the XUI call for a FileUri is indeed a valid function, what use is it if the resulting Uri fails for Android 7 and upwards? You'll forgive my caution - I was a bit taken aback to find the FileCopy member of the File Object class does not work as documented. With Android developments introducing such fundamental changes there seems to be a case for listing B4A functions which will no longer work, much as was done when Windows 7 rendered many API calls incompatible. That listing saved many of us from rushing up blind alleys!

I will pursue your FileProvider class which with its FileProviderUri offers a complete solution to my e-Mail attachment problem. (I still need to upgrade to B4A 9.90 so will do that first.) More research needed I guess, but I'm used to that. The first computers I programmed had thermionic valves in the days before transistors became common! It's been a 58 year learning curve.

Thanks again for your expertise.

Very Best,

David
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
One worry is I can't find anything about the FileProvider class in the documentation
FileProvider usage is demonstrated here: https://www.b4x.com/android/forum/threads/class-fileprovider-share-files.97865/

if the XUI call for a FileUri is indeed a valid function, what use is it if the resulting Uri fails for Android 7 and upwards?
This is not accurate. XUI.FileURI returns a url to a file resource. This is all that it does. It is useful for example, when you want to show a local html file in WebView.

Starting from Android 7, the OS doesn't allow apps to share files without a provider. The provider is FileProvider.
 
Upvote 0

Cantaflex

Member
This problem is so close to solution now I can hardly draw breath! Thanks to you guys I can now take a screenshot of any screen in the app (using FileProvider and Erel's takeScreenshot subroutine), attach it to an e-mail using a Uri and get a perfect reproduction on the recipient's computer. That's every screen displayed except the three reports, which are shown on-screen from the three tabs of a tabhost layout. These go through the motions without reporting an error, but the resulting JPEG file attached to the e-mail has no content. It seems the routines can't capture the actual display if it is inside a Tabhost. In some way it is masked. In every case, successful or not, I call takeScreenshot after creating the display in its layout (or the chart for the graph). Any suggestions on how to get past the Tabhost to the actual display, be it label or chart, would be very welcome.
 
Upvote 0

Cantaflex

Member
Further to the above, it isn't a critical issue, because I can always dispense with the Tabhost and set up my own reporting page with three buttons and three layouts which will do just about the same thing anyway and give me the desired result. If there is a way to capture the screen through the Tabhost though, it would be a preferred choice. Very Best again,

David
 
Upvote 0

Cantaflex

Member
I think to fix this issue, you will first need to make your app be a "File Provider":

Then instead of specifying a file "directory" to gmail for the attachment, you will instead provide the "FileURI" of the file to Gmail:

I can report complete success using FileProvider and adding attachments using the Uri for all three reports including the graph. The pics attached show how they appear to the e-mail recipient. Very clear and expressive. The problem with the chart turned out to be "too many panels" being nested in the construction. I cut it to one and the screen capture worked fine.

Thanks again for your morale-boosting help. Dare I suggest that whoever is developing the government's Test & Trace App would have a better solution if they handed the job to you guys!

Very Best,

David
 

Attachments

  • result.jpg
    result.jpg
    81.2 KB · Views: 180
  • detail (1).jpg
    detail (1).jpg
    91.8 KB · Views: 183
  • chart.jpg
    chart.jpg
    84.2 KB · Views: 178
Upvote 0

JohnC

Expert
Licensed User
Longtime User
And if you want to crop the image (for example to get rid of the "EMAIL" button at the bottom of the image) to produce a more focused image attachment, I just came across this thread:

 
Upvote 0

emexes

Expert
Licensed User
A thought in favor of sending the table in HTML format, is that the receiver can then easily copy-paste parts or all of it into a spreadsheet, and it will (well: should) retain the row/column structure.
 
Upvote 0

Cantaflex

Member
And if you want to crop the image (for example to get rid of the "EMAIL" button at the bottom of the image) to produce a more focused image attachment, I just came across this thread:

That's another good tip - thanks again JohnC. Am in the middle of writing the manual, but I'll certainly be refining these attachments as I get more used to the possibilities. I managed to get some semblance of control so far, but your suggestion gives me more options. I'll certainly pursue it. Also emexes note about HTML and the possibilities for transfer to spreadsheet will need a good look. All I need now is time!!
A thought in favor of sending the table in HTML format, is that the receiver can then easily copy-paste parts or all of it into a spreadsheet, and it will (well: should) retain the row/column structure.
Great idea again - time is all I need!
 
Upvote 0
Top