Xamarin. Forms =>Java语言类转换异常:'无法将android.net.Uri$HierarchicalUri强制转换为android.net.Uri[]'

gopyfrb3  于 2023-02-17  发布在  Java
关注(0)|答案(1)|浏览(130)

我在我的Web视图中使用,并在我的Xamarin Forms应用程序中使用此Web视图,我启用在Xamarin Forms中使用输入和在我的手机上打开的文件浏览器,但在我选择任何文件后,我得到了这个异常:Java.Lang.ClassCastException:“无法将android.net.Uri$分层结构Uri强制转换为android.net.Uri[]”;

protected override void OnActivityResult(int requestCode, Result resultCode, Intent intent)
        {
            if (requestCode == FILECHOOSER_RESULTCODE)
            {
                if (null == mUploadCallbackAboveL)
                    return;
                Java.Lang.Object result = intent == null || resultCode != Result.Ok
                    ? null
                    : intent.Data;
                mUploadCallbackAboveL.OnReceiveValue(result);
                mUploadCallbackAboveL = null;
            }
        }

enter image description here

pb3skfrl

pb3skfrl1#

最后我用以下代码修复了这个问题:

protected override void OnActivityResult(int requestCode, Result resultCode, Intent intent)
    {
        if (requestCode == FILECHOOSER_RESULTCODE)
        {
            if (resultCode == Result.Ok)
            {
                Android.Net.Uri result = intent.Data ?? ImageUri;
                if (UploadCallbackAboveL != null)
                {
                    UploadCallbackAboveL.OnReceiveValue(new Android.Net.Uri[] { result });
                }
            }
            else
            {
                UploadCallbackAboveL.OnReceiveValue(null);
            }
            UploadCallbackAboveL = null;
        }
        else
        {
            base.OnActivityResult(requestCode, resultCode, intent);
        }
    }

相关问题