Skip to content

Commit

Permalink
- added Android FileProvider support to handle file share panel on ta…
Browse files Browse the repository at this point in the history
…rgetSDK 28 and newer
  • Loading branch information
firestorm40 committed Aug 21, 2019
1 parent d16d1d7 commit 8791a1c
Show file tree
Hide file tree
Showing 5 changed files with 1,947 additions and 1,923 deletions.
35 changes: 11 additions & 24 deletions SPIXI/SPIXI.Android/Classes/FileOperations.cs
Expand Up @@ -13,6 +13,9 @@
using SPIXI.Droid.Classes;
using Xamarin.Forms;
using System.Threading.Tasks;
using Android.Support.V4.Content;
using Android.Support.Compat;
using Java.IO;

[assembly: Dependency(typeof(FileOperations_Android))]

Expand All @@ -26,30 +29,14 @@ public FileOperations_Android()

public Task share(string filepath, string title)
{
var extension = filepath.Substring(filepath.LastIndexOf(".") + 1).ToLower();
var contentType = string.Empty;

// You can manually map more ContentTypes here if you want.
switch (extension)
{
case "pdf":
contentType = "application/pdf";
break;
case "png":
contentType = "image/png";
break;
default:
contentType = "application/octetstream";
break;
}

var intent = new Intent(Intent.ActionSend);
intent.SetType(contentType);
intent.PutExtra(Intent.ExtraStream, Android.Net.Uri.Parse("file://" + filepath));
intent.PutExtra(Intent.ExtraText, string.Empty);
intent.PutExtra(Intent.ExtraSubject, string.Empty);

var chooserIntent = Intent.CreateChooser(intent, title ?? string.Empty);
File file = new File(filepath);
Intent shareIntent = new Intent();
shareIntent.SetAction(Intent.ActionSend);
shareIntent.SetType("application/octetstream");
Android.Net.Uri uriShare = FileProvider.GetUriForFile(_context, "com.ixian.provider", file);
shareIntent.PutExtra(Intent.ExtraStream, uriShare);

var chooserIntent = Intent.CreateChooser(shareIntent, title ?? string.Empty);
chooserIntent.SetFlags(ActivityFlags.ClearTop);
chooserIntent.SetFlags(ActivityFlags.NewTask);
_context.StartActivity(chooserIntent);
Expand Down
14 changes: 13 additions & 1 deletion SPIXI/SPIXI.Android/Properties/AndroidManifest.xml
Expand Up @@ -7,5 +7,17 @@
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application android:label="SPIXI"></application>
<application android:label="SPIXI">
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.ixian.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>


</application>
</manifest>

0 comments on commit 8791a1c

Please sign in to comment.