How to Open Files on Xamarin Android Using FilePicker
using Xamarin.Essentials; (at the top of the file)
async Task<FileResult> PickAndShow(PickOptions options)
{
try
{
var result = await FilePicker.PickAsync(options);
if (result != null)
{
string Text = $"File Name: {result.FileName}";
if (result.FileName.EndsWith("jpg", StringComparison.OrdinalIgnoreCase) ||
result.FileName.EndsWith("png", StringComparison.OrdinalIgnoreCase))
{
var stream = await result.OpenReadAsync();
Image = ImageSource.FromStream(() => stream);
}
}
return result;
}
catch (Exception ex)
{
// The user canceled or something went wrong
}
return null;
}
But I get this error:
The name FilePicker does not exist in the current context.
There are several other similar errors.
Any help is very appreciated.
Xamarin
-
Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 36,456 Reputation points • Microsoft Vendor
2023-03-22T05:18:08.69+00:00 With your code example, you've referenced the correct namespace, but VS still says that this class was not found.
For this problem, you can try deleting the
/bin
and/obj
folders of the project file, deletingXamarin.Essentials
in the project and reinstalling the new version of Nuget Package.Please reboot VS after that, will this issue occur?
Your prompt reply will be highly appreciated.
-
Ronald GANS 136 Reputation points
2023-03-23T01:30:27.5933333+00:00 I deleted Xamarin.Essentials from the Using statement and also from the references along with the bin and obj folders. Unloaded VS. Then restarted, added Xamarin.Essentials, cleaned and rebuilt all.
Same problem.
ImgeSource, FileResult, PickAndShow, Pickoptions and FilePicker are all marked the same: Do not exist in current context.
async Task<FileResult> PickAndShow(PickOptions options) { try { var result = await FilePicker.PickAsync(options); if (result != null) { string Text = $"File Name: {result.FileName}"; if (result.FileName.EndsWith("jpg", StringComparison.OrdinalIgnoreCase) || result.FileName.EndsWith("png", StringComparison.OrdinalIgnoreCase)) { var stream = await result.OpenReadAsync(); Android.Media.Image img = ImageSource.FromStream(() => stream); } } return result; } catch (Exception ex) { // The user canceled or something went wrong } return null; }
-
Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 36,456 Reputation points • Microsoft Vendor
2023-03-23T01:59:17.5766667+00:00 If you use namespaces directly, such as
Xamarin.Essentials.FilePicker
, could you get the FilePicker class?In addition, if your project is a Xamarin.Forms application, it is also possible that you do not have Essential Package installed in the
.Android
project.Was this information helpful to you? If not, could you please share a demo that minimizes the reproduction of this issue?
-
Ronald GANS 136 Reputation points
2023-03-27T21:54:51.8666667+00:00 I do not use the namespace directly.
I think I'm done trying to add file opening to my app. I know it's not impossible but I just can't figure how to do it.
-
Ronald GANS 136 Reputation points
2023-03-27T21:55:36.3633333+00:00 If I can see a complete example of this, that would be great.
-
Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 36,456 Reputation points • Microsoft Vendor
2023-03-28T07:50:41.9633333+00:00 Here are the steps for a simple instance of a
Filepciker
I created in Xamarin Android, which you could refer to.step 1: Add permission:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Step 2: Create a TextView for displaying result.
<TextView android:id="@+id/textView1" ... />
Step 3: Implement a FilePicker method that selects an image and displays the file name, and writes it to a button's click event:
async Task<FileResult> PickAndShow(PickOptions options) { try { var result = await FilePicker.PickAsync(options); if (result != null) { var textview = FindViewById(Resource.Id.textView1) as TextView; if (result.FileName.EndsWith("jpg", StringComparison.OrdinalIgnoreCase) || result.FileName.EndsWith("png", StringComparison.OrdinalIgnoreCase)) { textview.Text = result.FileName; } } return result; } catch (Exception ex) { // The user canceled or something went wrong } return null; } // On Click Event private async void OnClick(object sender, EventArgs eventArgs) { var customFileType = new FilePickerFileType(new Dictionary<DevicePlatform, IEnumerable<string>> { { DevicePlatform.Android, new[] { "image/*" } }, }); var options = new PickOptions { PickerTitle = "Please select a comic file", FileTypes = customFileType, }; await PickAndShow(options); }
Finally, you could add the onclick event above to a button.
Did this example help you?
Your prompt reply will be highly appreciated.
-
Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 36,456 Reputation points • Microsoft Vendor
2023-03-29T08:25:56.1533333+00:00 May I know whether your issue has been solved or not? If not, please share it in here. We can work together to figure it out.
-
Ronald GANS 136 Reputation points
2023-03-29T14:02:39.7066667+00:00 It has not been solved. But I am working on it.
-
Ronald GANS 136 Reputation points
2023-03-29T21:35:16.4+00:00 Same problem as before. The app fails to recognize FilePicker or anything related to that.
-
Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 36,456 Reputation points • Microsoft Vendor
2023-03-30T08:09:12.6766667+00:00 Thanks for your feedback.
If you use namespaces directly, such as
Xamarin.Essentials.FilePicker
, would you get theFilePicker
class?If the issue is still archived, can you give a link to a minimal example that reproduces the issue (e.g. a GitHub repository)?
-
Ronald GANS 136 Reputation points
2023-03-30T11:22:55.1733333+00:00 I tried that. No difference.
-
Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 36,456 Reputation points • Microsoft Vendor
2023-03-31T07:52:58.2733333+00:00 Since this issue cannot be reproduced, it is recommended that you create a support ticket for one-on-one technical support.
-
Ronald GANS 136 Reputation points
2023-03-31T10:59:32.9233333+00:00 That would be nice but I'm just a dev. Not a big company, so no support plan.
-
rmpQue 6 Reputation points
2023-08-23T08:00:50.74+00:00 I'm also facing an issue with FilePicker API only for Android 13.
The code is working fine below Android 12 but for Android 13 getting permission exceptions within FilePicker API.
Your prompt reply will be highly appreciated.
Xamarin.Essentials.PermissionException: StorageRead permission was not granted: Denied at Xamarin.Essentials.Permissions.EnsureGrantedAsync[TPermission] () [0x00066] in D:\a\1\s\Xamarin.Essentials\Permissions\Permissions.shared.cs:29 at Xamarin.Essentials.FilePicker.PlatformPickAsync (Xamarin.Essentials.PickOptions options, System.Boolean allowMultiple) [0x0001d] in D:\a\1\s\Xamarin.Essentials\FilePicker\FilePicker.android.cs:16 at Xamarin.Essentials.FilePicker.PickAsync (Xamarin.Essentials.PickOptions options) [0x0001c] in D:\a\1\s\Xamarin.Essentials\FilePicker\FilePicker.shared.cs:12
Sign in to comment