gpt4 book ai didi

c# - Xamarin中如何实现Android 5.0以上的打开文件选择器?

转载 作者:行者123 更新时间:2023-11-30 23:29:33 25 4
gpt4 key购买 nike

我使用下面的代码打开文件选择器,我不确定如何在 Android 5.0 及更高版本上实现它。

Action<IValueCallback, Java.Lang.String, Java.Lang.String> callback;

public FileChooserWebChromeClient(Action<IValueCallback, Java.Lang.String, Java.Lang.String> callback)
{
this.callback = callback;
}

//For Android 4.1
[Java.Interop.Export]
public void openFileChooser(IValueCallback uploadMsg, Java.Lang.String acceptType, Java.Lang.String capture)
{
callback(uploadMsg, acceptType, capture);
}

// For Android > 5.0
//I am stuck here, taken from Android
[Java.Interop.Export]
public bool onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams)
{
mUploadCallbackLollipop = filePathCallback;
openFileChooserActivity();
return true;
}

回调函数:

var chrome = new FileChooserWebChromeClient((uploadMsg, acceptType, capture) =>
{
MainActivity.UploadMessage = uploadMsg;
if(Build.VERSION.SdkInt < BuildVersionCodes.Kitkat)
{
var i = new Intent(Intent.ActionGetContent);

//To set all type of files
i.SetType("image/*");

//Here File Chooser dialog is started as Activity, and it gives result while coming back from that Activity.
((MainActivity)this.Context).StartActivityForResult(Intent.CreateChooser(i, "File Chooser"), MainActivity.GALLERY_INTENT_CALLED);
}
else
{
var i = new Intent(Intent.ActionOpenDocument);
i.AddCategory(Intent.CategoryOpenable);

//To set all type of files
i.SetType("image/*");

//Here File Chooser dialog is started as Activity, and it gives result while coming back from that Activity.
((MainActivity)this.Context).StartActivityForResult(Intent.CreateChooser(i, "File Chooser"), MainActivity.GALLERY_KITKAT_INTENT_CALLED);
}
});

如何实现Android 5.0以上的回调,请高手帮忙

最佳答案

以下代码在 WebView for Android 5.0 (Lollipop) 中打开文件选择器

// For Android > 5.0
[Android.Runtime.Register("onShowFileChooser", "(Landroid/webkit/WebView;Landroid/webkit/ValueCallback;Landroid/webkit/WebChromeClient$FileChooserParams;)Z", "GetOnShowFileChooser_Landroid_webkit_WebView_Landroid_webkit_ValueCallback_Landroid_webkit_WebChromeClient_FileChooserParams_Handler")]
public override Boolean OnShowFileChooser (Android.Webkit.WebView webView, IValueCallback uploadMsg, WebChromeClient.FileChooserParams fileChooserParams)
{
try
{
callback(uploadMsg, null, null);
}
catch(Exception e)
{

}
return true;
}

回调可以和其他版本一样实现,但是不能直接在OnReceiveValue方法中传递路径。它应该按如下方式传递:

UploadMessage.OnReceiveValue(WebChromeClient.FileChooserParams.ParseResult(Convert.ToInt32(resultCode), intent));

我在使用以下用于旧版本的函数时出现类型转换异常。

UploadMessage.OnReceiveValue(Android.Net.Uri.Parse(string.Format("file://{0}", result.ToString())));

引用:

From Xamarin Documentation

来自 Android 源代码:

https://github.com/anthonycr/Lightning-Browser/issues/253

关于c# - Xamarin中如何实现Android 5.0以上的打开文件选择器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35341665/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com