Issue
When I attach pdf and doc files using Recent files I'm getting the following exception. Can anyone help on this. Working fine when i attach image file using Recent files please help on this I m new to this Xamarin forms.
Thanks in advance.
at Java.Interop.JniEnvironment+InstanceMethods.CallNonvirtualObjectMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniObjectReference type, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x0008e] in <bd6bd528a8784b7caf03e9f25c9f0d7b>:0
at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeNonvirtualObjectMethod (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters) [0x0001f] in <bd6bd528a8784b7caf03e9f25c9f0d7b>:0
at Android.Content.ContentResolver.Query (Android.Net.Uri uri, System.String[] projection, System.String selection, System.String[] selectionArgs, System.String sortOrder) [0x000a0] in /Users/builder/azdo/_work/1/s/xamarin-android/src/Mono.Android/obj/Release/monoandroid10/android-28/mcw/Android.Content.ContentResolver.cs:1098
at Plugin.FilePicker.IOUtil.GetDataColumn (Android.Content.Context context, Android.Net.Uri uri, System.String selection, System.String[] selectionArgs) [0x00013] in D:\a\1\s\src\Plugin.FilePicker\Android\IOUtil.android.cs:154
at Plugin.FilePicker.IOUtil.GetPath (Android.Content.Context context, Android.Net.Uri uri) [0x0017d] in D:\a\1\s\src\Plugin.FilePicker\Android\IOUtil.android.cs:111
at Plugin.FilePicker.FilePickerActivity.OnActivityResult (System.Int32 request Code, Android.App.Result resultCode, Android.Content.Intent data) [0x00054] in D:\a\1\s\src\Plugin.FilePicker\Android\FilePickerActivity.android.cs:168
--- End of stack trace from previous location where exception was thrown ---
at Plugin.FilePicker.FilePickerImplementation.PickFile (System.String[] allowedTypes) [0x00028] in D:\a\1\s\src\Plugin.FilePicker\Android\FilePickerImplementation.android.cs:60
at FileUploadPOC.MainPage.PickAndShow (System.String[] fileTypes) [0x0002e] in C:\Users\Gadda.Raju\Downloads\XamarinForms-FilePicker-master\FileUploadPOC\MainPage.xaml.cs:47
--- End of managed Java.Lang.NullPointerException stack trace ---
java.lang.NullPointerException: uri
at java.util.Objects.requireNonNull(Objects.java:245)
at android.content.ContentResolver.query(ContentResolver.java:1166)
at android.content.ContentResolver.query(ContentResolver.java:1124)
at android.content.ContentResolver.query(ContentResolver.java:1080)
at crc64424a8adc5a1fbe28.FilePickerActivity.n_onActivityResult(Native Method)
at crc64424a8adc5a1fbe28.FilePickerActivity.onActivityResult(FilePickerActivity.java:47)
at android.app.Activity.dispatchActivityResult(Activity.java:8469)
at android.app.ActivityThread.deliverResults(ActivityThread.java:5364)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:5412)
at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2317)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:255)
at android.app.ActivityThread.main(ActivityThread.java:8192)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:632)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1049)
} ```
**The following code im using**
``` private async Task PickAndShow(string[] fileTypes)
{
try
{
var pickedFile = await CrossFilePicker.Current.PickFile(fileTypes);
if (pickedFile != null)
{
lblName.Text = pickedFile.FileName;
lblFilePath.Text = pickedFile.FilePath;
}
}
catch (Exception ex)
{
}
} ```
Solution
Yes, from Xamarin.Plugin.FilePicker,we know that :
This package has been deprecated as it is legacy and is no longer maintained.
Just as SushiHangover said, you can use Xamarin.Essentials: File Picker to achieve this.
The FilePicker
class lets a user pick a single or multiple files from the device.
FilePicker.PickAsync()
method enables your user to pick a file from the device. You are able to specific different PickOptions
when calling the method enabling you to specify the title to display and the file types the user is allowed to pick. By default.
You can refer to the following sample code:
async Task<FileResult> PickAndShow(PickOptions options)
{
try
{
var result = await FilePicker.PickAsync(options);
if (result != null)
{
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;
}
Default file types are provided with FilePickerFileType.Images
, FilePickerFileType.Png
, and FilePickerFilerType.Videos
. You can specify custom files types when creating the PickOptions
and they can be customized per platform.
For more,check:https://docs.microsoft.com/en-us/xamarin/essentials/file-picker?context=xamarin%2Fxamarin-forms&tabs=android .
Update:
For how to open an existing file, you can refer: Xamarin.Essentials; FilePicker; Save a new file .
Answered By - Jessie Zhang -MSFT
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.