Skip to main content

Share Files & Text with Other Apps

Share Files & Text with Other Apps

Use this component to share any files or text with apps on the device.

To get started using it, you must use KmpShare.

Share Plain text

To share any plain text with any app, please use the following function


_1
KmpShare.shareTextWithAnyApp(text: String, optionalTitle: String = "")

Share Files

In order to share files you must configure your file type. You can do so via method chaining like so:


_3
KmpShare
_3
.setFileType(Mimes.csv) // set the file type (Mimes is an object provided by KmpEssentials to simplify configuration)
_3
.shareFileWithAnyApp(filePath: String, optionalTitle: String = "")

Custom Intent Flags (Android Only)

You also have the option to add your own custom Intent Flags like so:


_4
KmpShare
_4
.setFileType(Mimes.csv)
_4
.addOptionalFlags(Intent.ACTION_PICK)
_4
.shareFileWithAnyApp(filePath: String, optionalTitle: String = "")

If you want to reset this configuration, please use:


_8
KmpShare
_8
.setFileType(Mimes.csv)
_8
.addOptionalFlags(Intent.ACTION_PICK)
_8
_8
_8
// resets and removes (Intent.ACTION_PICK) before showing the IntentChooser
_8
.resetOptionalFlags()
_8
.shareFileWithAnyApp(filePath: String, optionalTitle: String = "")

Android Setup

Before being able to share files you must configure the FileProvider that KmpEssentials requires to provide access to other apps, the files that you will be sharing with them.

In your manifest file please paste the following inside the application tag.


_17
<application
_17
android:supportsRtl="true"
_17
android:theme="@style/AppTheme">
_17
_17
<provider
_17
android:name="androidx.core.content.FileProvider"
_17
android:authorities="${applicationId}.fileprovider"
_17
android:grantUriPermissions="true"
_17
android:exported="false">
_17
<meta-data
_17
android:name="android.support.FILE_PROVIDER_PATHS"
_17
android:resource="@xml/file_paths" />
_17
</provider>
_17
_17
....
_17
_17
</application>

You must also add an xml file inside the xml folder of your android resource directory. This file will be used by the file provider to scope and control which directories the external apps have access to when sharing files.

Because the url being passed into KmpEssentials can be anywhere or anything, you will need to configure the directories the file provider has access to. Here (as an example) we are providing access to external storage directory, scoped to the app's package Id.

This means the file provider will provide access to any app, to use any file used in this directory.


_4
<?xml version="1.0" encoding="utf-8"?>
_4
<paths>
_4
<external-cache-path name="projection" path="." />
_4
</paths>

For better security, it's better if you create a directory inside external storage (or the root directory you are configuring in the xml file), and specify that any app only has access to a specific subdirectory instead of the root directory. So for example, if I have a folder called devices inside my external storage directory scoped to my package, I would use:


_4
<?xml version="1.0" encoding="utf-8"?>
_4
<paths>
_4
<external-cache-path name="projection" path="devices/" />
_4
</paths>

Then only the files inside externalStorage/devices will be accessible to Sharing.

You can read more on the available directories of FileProvider here

iOS Setup

None required