android webview中的文件选择器仅工作一次
我需要从webview中选择一个图像并上传到服务器上。代码可以工作,但如果我按back键而不选择任何内容,下次控件不进入ShowFileChooser上的android webview中的文件选择器仅工作一次,android,webview,Android,Webview,我需要从webview中选择一个图像并上传到服务器上。代码可以工作,但如果我按back键而不选择任何内容,下次控件不进入ShowFileChooser上的 我已经在android浏览器上检查了同样的功能,它在那里工作,所以一定有我遗漏的东西 代码如下: web.setWebChromeClient(new BizzerClient()); web.setWebViewClient(new WebViewClient(){ //==============
我已经在android浏览器上检查了同样的功能,它在那里工作,所以一定有我遗漏的东西
代码如下:
web.setWebChromeClient(new BizzerClient());
web.setWebViewClient(new WebViewClient(){
//=========================================
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
if(url.contains(Domain) && !url.startsWith(SMS)){
//== if url is of same site and not related to sms, do nothing
}else{
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
return true;
}
return false;
}
});
//=========================================
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if(requestCode==FileChooser){
Uri result = intent == null || resultCode != RESULT_OK ?null:Uri.parse(intent.getDataString());
if(result==null) return;
if(fPathCallback!=null){
fPathCallback.onReceiveValue(new Uri[]{result});
fPathCallback = null;
}else{
upload.onReceiveValue(result);
upload = null;
}
}
}
//=====================================================
class BizzerClient extends WebChromeClient{
//=========================================
public void openFileChooser(ValueCallback<Uri> uploadMsg) {
upload = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
startActivityForResult(Intent.createChooser(i,"File Chooser"), FileChooser);
}
//=========================================
//== For Android 3.0+
public void openFileChooser( ValueCallback uploadMsg, String acceptType ) {
upload = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("*/*");
startActivityForResult(Intent.createChooser(i, "File Browser"),FileChooser);
}
//=========================================
//== For Android 4.1
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture){
upload = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
startActivityForResult( Intent.createChooser( i, "File Chooser" ), FileChooser );
}
//=========================================
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback,WebChromeClient.FileChooserParams fileChooserParams) {
fPathCallback = filePathCallback;
Toast.makeText(getApplicationContext(), "A", Toast.LENGTH_SHORT).show();
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
startActivityForResult(Intent.createChooser(i,"File Chooser"), FileChooser);
return true;
}
}
web.setWebChromeClient(新BizzerClient());
setWebViewClient(新的WebViewClient(){
//=========================================
@凌驾
公共布尔值shouldOverrideUrlLoading(WebView视图,字符串url){
if(url.contains(域)和&!url.startsWith(SMS)){
//==如果url属于同一站点且与sms无关,则不执行任何操作
}否则{
意向i=新意向(意向.行动\视图);
i、 setData(Uri.parse(url));
星触觉(i);
返回true;
}
返回false;
}
});
//=========================================
@凌驾
ActivityResult上受保护的void(int-requestCode、int-resultCode、Intent-Intent){
如果(requestCode==FileChooser){
Uri result=intent==null | | resultCode!=result_OK?null:Uri.parse(intent.getDataString());
if(result==null)返回;
if(fPathCallback!=null){
fPathCallback.onReceiveValue(新Uri[]{result});
fPathCallback=null;
}否则{
upload.onReceiveValue(结果);
上传=空;
}
}
}
//=====================================================
类BizzerClient扩展WebChromeClient{
//=========================================
public void openFileChooser(ValueCallback uploadMsg){
上传=上传消息;
意向i=新意向(意向.行动\u获取\u内容);
i、 addCategory(意图。类别可打开);
i、 setType(“image/*”);
startActivityForResult(Intent.createChooser(i,“文件选择器”),FileChooser);
}
//=========================================
//==适用于Android 3.0+
public void openFileChooser(ValueCallback uploadMsg,String acceptType){
上传=上传消息;
意向i=新意向(意向.行动\u获取\u内容);
i、 addCategory(意图。类别可打开);
i、 集合类型(“*/*”);
startActivityForResult(Intent.createChooser(i,“文件浏览器”)、FileChooser;
}
//=========================================
//==对于Android 4.1
public void openFileChooser(ValueCallback uploadMsg、字符串接受类型、字符串捕获){
上传=上传消息;
意向i=新意向(意向.行动\u获取\u内容);
i、 addCategory(意图。类别可打开);
i、 setType(“image/*”);
startActivityForResult(Intent.createChooser(i,“文件选择器”),FileChooser);
}
//=========================================
公共布尔onShowFileChooser(WebView WebView、ValueCallback filePathCallback、WebChromeClient.FileChooseParams FileChooseParams){
fPathCallback=filePathCallback;
Toast.makeText(getApplicationContext(),“A”,Toast.LENGTH\u SHORT.show();
意向i=新意向(意向.行动\u获取\u内容);
i、 addCategory(意图。类别可打开);
i、 setType(“image/*”);
startActivityForResult(Intent.createChooser(i,“文件选择器”),FileChooser);
返回true;
}
}
有人知道如何解决这个问题吗?我的同事也有同样的问题,他已经通过
upload.onReceiveValue(null)
我也有同样的问题,问题是我期待一个OnActivityResult,但是当你按下back按钮时,它没有被触发
解决方案在onResume上实现了以下代码,以告知回调答案为空并能够重用它:
@Override
protected void onResume() {
super.onResume();
if (fPathCallback == null)
return;
fPathCallback.onReceiveValue(new Uri[]{});
fPathCallback = null;
}
我在onActivityResult上添加了相同的代码&它可以工作&你是个救生员