我需要从webview中选择一张图片并将其上传到服务器上。该代码有效,但如果我在没有选择任何内容的情况下按回,则下次控件不会进入onShowFileChooser
。
我在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;
}
}
有谁知道如何解决这个问题?
答案 0 :(得分:8)
我的同事有完全相同的问题,他已经修复了
upload.onReceiveValue(空)
答案 1 :(得分:1)
我遇到了同样的问题,问题是我希望看到一个OnActivityResult,但是当您按下“后退”按钮时,却没有触发。
该解决方案是在onResume上实现以下代码,以告知回调答案为空并且可以重复使用:
@Override
protected void onResume() {
super.onResume();
if (fPathCallback == null)
return;
fPathCallback.onReceiveValue(new Uri[]{});
fPathCallback = null;
}