经过一些练习后,我可以使用 openFileChooser() 从手机中选择一个文件,然后通过 OnActivityResult() 获得结果。我的问题是,打开文件选择器后,我的片段转到 OnPause(),选择文件后,我丢失了 webview 内容,它转到 OnStart()。我不想重新加载整个页面。有什么办法可以保留以前的内容并将选定的文件添加到 webview 中?
我重申我可以正确获取选定的文件内容。我的问题出在 OnActivityResult() 之后调用的 Onstart() 方法中。我不知道为什么它没有保留我在 OnCreateView() 上写的内容。这是我的代码:
private View _createdView;
private InternalWebView _client;
private UserControls.ChromeClient _chromeClient;
private WebView _webview;
private Fragment_Progressbar _fragment_progressbar;
private bool _isFromFileSelection = false;
private Android.Net.Uri selectedFile;
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
_createdView = CreateAndConfigLayout(inflater, container, Resource.Layout.WebContainer, Resource.Id.WebviewMainContainer);
_webview = _createdView.FindViewById<WebView>(Resource.Id.webView1);
_webview.Touch += webview_Touch;
_webview.Click += webview_Click;
if (_client == null)
{
_client = new InternalWebView(SecurityPreference.Instance.GetLastCorrectUseName(), SecurityPreference.Instance.GetLastCorrectPassword());
}
if (_chromeClient == null)
{
_chromeClient = new UserControls.ChromeClient(this);
}
_fragment_progressbar = new Fragment_Progressbar(Activity, 1200);
_fragment_progressbar.startLoad();
RefreshData();
return _createdView;
}
public override void OnActivityResult(int requestCode, int resultCode, Intent data)
{
if (data != null)
{
if (requestCode == 1)
{
if (null == UploadMessage || data == null)
return;
selectedFile = data == null || resultCode != 1 ? null : data.Data;
UploadMessage.OnReceiveValue(selectedFile);
UploadMessage = null;
}
}
}
public override void OnStart()
{
base.OnStart();
}
public override void OnPause()
{
base.OnPause();
}
public override void RefreshData()
{
if (this.Arguments != null || this.Arguments.ContainsKey("Address"))
{
var addrress = this.Arguments.GetString("Address");
if (!string.IsNullOrEmpty(addrress))
{
_webview.SetWebViewClient(_client);
_webview.SetWebChromeClient(_chromeClient);
_webview.Settings.JavaScriptEnabled = true;
_webview.Settings.PluginsEnabled = true;
_webview.Settings.SetAppCacheEnabled(true);
_webview.Settings.DomStorageEnabled = true;
_webview.Settings.AllowContentAccess = true;
_webview.Settings.AllowFileAccessFromFileURLs = true;
_webview.Settings.AllowUniversalAccessFromFileURLs = true;
_webview.Settings.AllowFileAccess = true;
var serverAddress = Configurations.GetServerAddress();
if (addrress.StartsWith(serverAddress))
{
var resultAddress = Configurations.GetRedirectAddress(addrress);
_webview.PostUrl(resultAddress, EncodingUtils.GetBytes("Username=" + SecurityPreference.Instance.GetLastCorrectUseName() + "&password=" + SecurityPreference.Instance.GetLastCorrectPassword(), "BASE64"));
_webview.SetDownloadListener(new MyDownloadListener(MainFragment));
}
else
{
_webview.LoadUrl(addrress);
}
}
}
SetHeaderTitle();
}
编辑:
这是我的 openFileChooser 方法:
public void openFileChooser(IValueCallback uploadMsg, String acceptType, String capture)
{
mUploadMessage = uploadMsg;
webFragment.UploadMessage = uploadMsg;
Intent i = new Intent(Intent.ActionGetContent);
i.AddCategory(Intent.CategoryOpenable);
i.SetType("*/*");
webFragment.Activity.StartActivityForResult(Intent.CreateChooser(i, "Choose File"), FILECHOOSER_RESULTCODE);
}