Recent

Author Topic: Share file from internal storage  (Read 617 times)

dseligo

  • Hero Member
  • *****
  • Posts: 1374
Share file from internal storage
« on: August 08, 2024, 02:25:03 pm »
I am trying to share file from internal storage (as an email attachment or with Whatsapp or similar, I don't care at this point).

I tried to mimic accepted answer from here: https://stackoverflow.com/questions/47955490/share-text-file-via-intent-from-internal-storage.

So I created a file res/xml/provider_paths.xml with following content:
Code: XML  [Select][+][-]
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <paths xmlns:android="http://schemas.android.com/apk/res/android">
  3.     <files-path name="files" path="." />
  4. </paths>

To the AndroidManifest.xml I added:
Code: XML  [Select][+][-]
  1. ...
  2.  
  3. <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
  4.  
  5. ...
  6.  
  7. <application>
  8.  
  9. ...
  10.  
  11.     <provider
  12.        android:name="androidx.core.content.FileProvider"
  13.        android:authorities="${applicationId}.fileprovider"
  14.        android:exported="false"
  15.        android:grantUriPermissions="true">
  16.         <meta-data
  17.            android:name="android.support.FILE_PROVIDER_PATHS"
  18.            android:resource="@xml/provider_paths" />
  19.     </provider>
  20.   </application>
  21.  
  22. ...
  23.  
  24.   <queries>
  25.     <intent>
  26.       <action android:name="android.intent.action.SEND"/>
  27.       <data android:scheme="*"/>
  28.     </intent>
  29.   </queries>
  30. </manifest>

And then tried this code:
Code: Pascal  [Select][+][-]
  1. procedure TAndroidModule1.Button1Click(Sender: TObject);
  2. const cFile = 'proba.txt';
  3. var Uri: jObject;
  4.     sFullPath: String;
  5.     f: TextFile;
  6. begin
  7.   sFullPath := GetEnvironmentDirectoryPath(dirInternalAppStorage)  + '/' + cFile;
  8.  
  9.   // create file if necessary
  10.   if not FileExists(sFullPath) then
  11.   begin
  12.     AssignFile(f, sFullPath);
  13.     Rewrite(f);
  14.     WriteLn(f, FormatDateTime('hh:nn:ss', Now));
  15.     WriteLn(f, 'proba1.');
  16.     CloseFile(f);
  17.   end;
  18.  
  19.   Uri := GetUriFromFile(sFullPath);
  20.   jIntentManager1.SetAction('android.intent.action.SEND');
  21.   jIntentManager1.PutExtraText('test');
  22.   jIntentManager1.PutExtraFile(Uri);
  23.   jIntentManager1.AddFlag(ifGrantReadUriPermission);
  24.   jIntentManager1.SetMimeType('plain/*');
  25.  
  26.   if jIntentManager1.ResolveActivity then
  27.     If jIntentManager1.StartActivity('Send Email ...') then
  28.       ShowMessage('Activity started')
  29.     else
  30.       ShowMessage('Activity not started')
  31.   else
  32.     ShowMessage('Not resolved');
  33. end;

I get 'Activity not started'.

Then I changed code to:
Code: Pascal  [Select][+][-]
  1. ...
  2.  
  3.   Uri := GetUriFromFile(sFullPath);
  4.   jIntentManager1.SetAction('android.intent.action.SEND');
  5.   jIntentManager1.SetMimeType('text/plain'); // jIntentManager1.SetMimeType('message/rfc822');
  6.  
  7.   // if I don't have those 3 lines I get message 'Activity not started' below
  8.   jIntentManager1.SetDataUri(jIntentManager1.GetMailtoUri('mymail@mymail.com'));
  9.   jIntentManager1.PutExtraMailSubject('Test 1');
  10.   jIntentManager1.PutExtraMailBody('This is test 1.' + LineEnding + LineEnding + 'Regards');
  11.  
  12.   jIntentManager1.PutExtraFile(Uri);
  13.  
  14.   jIntentManager1.AddFlag(ifActivityNewTask);
  15.   jIntentManager1.AddFlag(ifGrantReadUriPermission);
  16.  
  17.   if jIntentManager1.ResolveActivity then
  18.     If jIntentManager1.StartActivity('Send Email ...') then
  19. ...

Now the activity is started (Outlook is shown with mail, subject and body), but I get message 'Unable to add attachment'.

What can I do?
Does some have some example how to share/send attachment?

 

TinyPortal © 2005-2018