Skip to content Skip to sidebar Skip to footer

Share File To Another App (whatsapp, Telegram, Gmail)

I'm developing an android app using Xamarin.Forms. So far i did not found too much difficulties during the development, but now i think i'm missing something. I would like to: sha

Solution 1:

In the end i came up with a solution. I removed the intents from the manifest, and coded them in the main activity like this:

[IntentFilter(
new[] { Intent.ActionView }, 
Categories= new [] { Intent.CategoryBrowsable, Intent.CategoryDefault }, 
DataMimeType="text/plain",
DataPathPatterns= new []
{
    @".*\\.sl",
    @".*\\..*\\.sl",
    @".*\\..*\\..*\\.sl",
    @".*\\..*\\..*\\..*\\.sl"
})]
[IntentFilter(
new[] { Intent.ActionSend },
Categories= new[] { Intent.CategoryBrowsable, Intent.CategoryDefault },
DataMimeType="text/plain",
DataPathPatterns= new []
{
    @".*\\.sl",
    @".*\\..*\\.sl",
    @".*\\..*\\..*\\.sl",
    @".*\\..*\\..*\\..*\\.sl"
})]

also changed the Share.Show method to use the uri from the fileprovider:

publicvoid Show(string title, string filePath)
    {
        if(!_fsDroid.FileExists(filePath))
            return;

        var fileUri = FileProvider.GetUriForFile(_context, "com.***********.******.fileprovider", new File(filePath));
        var intent = new Intent(Intent.ActionSend);
        intent.SetType("text/plain");
        intent.PutExtra(Intent.ExtraText, string.Empty);
        intent.PutExtra(Intent.ExtraStream, fileUri);
        intent.SetData(fileUri);
        intent.SetFlags(ActivityFlags.GrantReadUriPermission);
        intent.SetFlags(ActivityFlags.ClearTop);
        intent.SetFlags(ActivityFlags.NewTask);

        var chooserIntent = Intent.CreateChooser(intent, title ?? string.Empty);
        _context.StartActivity(chooserIntent);
    }

In the manifest, inside the application tag i added

<providerandroid:name="android.support.v4.content.FileProvider"android:authorities="com.***********.******.fileprovider"android:exported="false"android:grantUriPermissions="true"><meta-dataandroid:name="android.support.FILE_PROVIDER_PATHS"android:resource="@xml/file_paths" /></provider>

and this is the content of my file_paths.xml

<pathsxmlns:android="http://schemas.android.com/apk/res/android"><files-pathname="exports"path="exports/"/></paths>

And now my application exports .sl file, and whatsapp, telegram, gmail and any other app that support text/plain file accepts the attachments.

Also, when opening/sending the .sl file my app is listed and usable as default for this kind of file.

Hope this helps out someone else in the future :)

Post a Comment for "Share File To Another App (whatsapp, Telegram, Gmail)"