无法使用 Xamarin.Plugin.Filepicker 使用最近的文件添加 pdf 和 doc 文件

Unable to add pdf and doc files using Recent files using Xamarin.Plugin.Filepicker

当我使用最近的文件附加 pdf 和 doc 文件时,出现以下异常。谁能帮忙解决这个当我使用最近的文件附加图像文件时工作正常 请对此提供帮助我是这个 Xamarin 表单的新手。

提前致谢。

  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\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\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\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\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)
            {
                
            }
        } ```

是的,从Xamarin.Plugin.FilePicker我们知道:

This package has been deprecated as it is legacy and is no longer maintained.

正如SushiHangover所说,可以用Xamarin.Essentials: File Picker来实现。

FilePicker class 允许用户从设备中选择一个或多个文件。

FilePicker.PickAsync() 方法使您的用户可以从设备中选择一个文件。您可以在调用该方法时指定不同的 PickOptions,从而使您能够指定要显示的标题和允许用户选择的文件类型。默认情况下。

您可以参考下面的示例代码:

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;
}

默认文件类型由 FilePickerFileType.ImagesFilePickerFileType.PngFilePickerFilerType.Videos 提供。您可以在创建 PickOptions 时指定自定义文件类型,它们可以根据平台进行自定义。

更多信息,请查看:https://docs.microsoft.com/en-us/xamarin/essentials/file-picker?context=xamarin%2Fxamarin-forms&tabs=android .

更新:

如何打开已有文件,可参考:Xamarin.Essentials; FilePicker; Save a new file .