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:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="files" path="." />
</paths>
To the AndroidManifest.xml I added:
...
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
...
<application>
...
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
</application>
...
<queries>
<intent>
<action android:name="android.intent.action.SEND"/>
<data android:scheme="*"/>
</intent>
</queries>
</manifest>
And then tried this code:
procedure TAndroidModule1.Button1Click(Sender: TObject);
const cFile = 'proba.txt';
var Uri: jObject;
sFullPath: String;
f: TextFile;
begin
sFullPath := GetEnvironmentDirectoryPath(dirInternalAppStorage) + '/' + cFile;
// create file if necessary
if not FileExists(sFullPath) then
begin
AssignFile(f, sFullPath);
Rewrite(f);
WriteLn(f, FormatDateTime('hh:nn:ss', Now));
WriteLn(f, 'proba1.');
CloseFile(f);
end;
Uri := GetUriFromFile(sFullPath);
jIntentManager1.SetAction('android.intent.action.SEND');
jIntentManager1.PutExtraText('test');
jIntentManager1.PutExtraFile(Uri);
jIntentManager1.AddFlag(ifGrantReadUriPermission);
jIntentManager1.SetMimeType('plain/*');
if jIntentManager1.ResolveActivity then
If jIntentManager1.StartActivity('Send Email ...') then
ShowMessage('Activity started')
else
ShowMessage('Activity not started')
else
ShowMessage('Not resolved');
end;
I get 'Activity not started'.
Then I changed code to:
...
Uri := GetUriFromFile(sFullPath);
jIntentManager1.SetAction('android.intent.action.SEND');
jIntentManager1.SetMimeType('text/plain'); // jIntentManager1.SetMimeType('message/rfc822');
// if I don't have those 3 lines I get message 'Activity not started' below
jIntentManager1.SetDataUri(jIntentManager1.GetMailtoUri('mymail@mymail.com'));
jIntentManager1.PutExtraMailSubject('Test 1');
jIntentManager1.PutExtraMailBody('This is test 1.' + LineEnding + LineEnding + 'Regards');
jIntentManager1.PutExtraFile(Uri);
jIntentManager1.AddFlag(ifActivityNewTask);
jIntentManager1.AddFlag(ifGrantReadUriPermission);
if jIntentManager1.ResolveActivity then
If jIntentManager1.StartActivity('Send Email ...') then
...
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?