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:
- share a custom file (*.sl) from my app to any app that can handle Attachments (like whatsapp, telegram, gmail.. etc)
- Open my custom file (*.sl) with my app
So, i digged a bit around the web and ended up with this;
In my AndroidManifest.xml to handle my custom file
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="file" />
<data android:host="*" />
<data android:mimeType="*/*" />
<data android:pathPattern=".*\.sl" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="content" />
<data android:mimeType="application/octet-stream" />
<data android:mimeType="application/sl" />
</intent-filter>
And this class, used as a Service from my Xamarin.Forms pcl
public class ShareDroid : IShare
{
private Context _context;
private FileSystemExDroid _fsDroid;
public ShareDroid()
{
_context = Android.App.Application.Context;
_fsDroid = new FileSystemExDroid();
}
public void Show(string title, string message, string filePath)
{
if(!_fsDroid.FileExists(filePath))
return;
var extension = filePath.Substring(filePath.LastIndexOf(".", StringComparison.Ordinal) + 1).ToLower();
var contentType = GetContentType(extension);
var intent = new Intent(Intent.ActionSend);
intent.SetType(contentType);
intent.PutExtra(Intent.ExtraStream, Uri.Parse("file://" + filePath));
intent.PutExtra(Intent.ExtraText, string.Empty);
intent.PutExtra(Intent.ExtraSubject, message ?? string.Empty);
var chooserIntent = Intent.CreateChooser(intent, title ?? string.Empty);
chooserIntent.SetFlags(ActivityFlags.ClearTop);
chooserIntent.SetFlags(ActivityFlags.NewTask);
_context.StartActivity(chooserIntent);
}
private static string GetContentType(string extension)
{
string contentType;
switch (extension)
{
case "pdf":
contentType = "application/pdf";
break;
case "png":
contentType = "image/png";
break;
case "sl":
contentType = "application/sl";
break;
default:
contentType = "application/octet-stream";
break;
}
return contentType;
}
}
And here i call the Show method of my share service
private void OnShare()
{
var fileSystem = DependencyService.Get<IFileSystemEx>();
var serializer = DependencyService.Get<ISerializer>();
var share = DependencyService.Get<IShare>();
var fileName = $"{Name}_{Id}.sl";
var path = fileSystem.CreateFilePath(fileName, SpecialFolder.Personal);
serializer.Save(path, Model);
share.Show(Resources.ShareViaLabel, Resources.ShareViaMessage, path);
}
When i test the app on any device (not emulated) i get the same result.
- Gmail says "Permission denied for the attachment"
- WhatsApp and Telegram says "Attachment not supported"
The Resources.ShareViaMessage (which is a little description) gets printed in the target app without problem.
The custom file is actually a .txt file with a custom extension (.sl).
Anyone can help?