Skip to content
This repository has been archived by the owner on May 15, 2024. It is now read-only.

Commit

Permalink
Rework the "picker" results to correctly manage files
Browse files Browse the repository at this point in the history
  • Loading branch information
mattleibow committed Nov 30, 2020
1 parent 439839d commit 4e3d487
Show file tree
Hide file tree
Showing 8 changed files with 425 additions and 202 deletions.
9 changes: 9 additions & 0 deletions Samples/Samples.Android/Properties/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,35 @@
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<queries>
<!-- Email -->
<intent>
<action android:name="android.intent.action.SENDTO" />
<data android:scheme="mailto" />
</intent>
<!-- Browser -->
<intent>
<action android:name="android.intent.action.VIEW" />
<data android:scheme="http" />
</intent>
<!-- Browser -->
<intent>
<action android:name="android.intent.action.VIEW" />
<data android:scheme="https" />
</intent>
<!-- Sms -->
<intent>
<action android:name="android.intent.action.VIEW" />
<data android:scheme="smsto" />
</intent>
<!-- PhoneDialer -->
<intent>
<action android:name="android.intent.action.DIAL" />
<data android:scheme="tel" />
</intent>
<!-- MediaPicker -->
<intent>
<action android:name="android.media.action.IMAGE_CAPTURE" />
</intent>
</queries>
<uses-feature android:name="android.hardware.location" android:required="false" />
<uses-feature android:name="android.hardware.location.gps" android:required="false" />
Expand Down
36 changes: 20 additions & 16 deletions Xamarin.Essentials/FilePicker/FilePicker.android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Android.App;
using Android.Content;
using Android.Provider;

namespace Xamarin.Essentials
{
Expand All @@ -33,23 +30,30 @@ static async Task<IEnumerable<FileResult>> PlatformPickAsync(PickOptions options

try
{
var result = await IntermediateActivity.StartAsync(pickerIntent, Platform.requestCodeFilePicker);
var resultList = new List<FileResult>();

var clipData = new List<global::Android.Net.Uri>();

if (result.ClipData == null)
void OnResult(Intent intent)
{
clipData.Add(result.Data);
}
else
{
for (var i = 0; i < result.ClipData.ItemCount; i++)
clipData.Add(result.ClipData.GetItemAt(i).Uri);
// The uri returned is only temporary and only lives as long as the Activity that requested it,
// so this means that it will always be cleaned up by the time we need it because we are using
// an intermediate activity.

if (intent.ClipData == null)
{
var path = FileSystem.EnsurePhysicalPath(intent.Data);
resultList.Add(new FileResult(path));
}
else
{
for (var i = 0; i < intent.ClipData.ItemCount; i++)
{
var uri = intent.ClipData.GetItemAt(i).Uri;
var path = FileSystem.EnsurePhysicalPath(uri);
resultList.Add(new FileResult(path));
}
}
}

foreach (var contentUri in clipData)
resultList.Add(new FileResult(contentUri));
await IntermediateActivity.StartAsync(pickerIntent, Platform.requestCodeFilePicker, onResult: OnResult);

return resultList;
}
Expand Down
Loading

0 comments on commit 4e3d487

Please sign in to comment.