Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FilePicker from android webview working only once

I need to pick an image from webview and upload it onto server. The code works, but if I press back without picking anything, next time the control does not goes in onShowFileChooser.

I have checked the same on android browser and it works there, so there has to be something that I am missing.

Below is the code:

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;
        }
    }

Does anyone know how to fix this?

like image 549
viv Avatar asked Mar 30 '15 13:03

viv


2 Answers

My workmate has exactly same issue, he has fixed it by

upload.onReceiveValue(null)

like image 95
Allen Avatar answered Oct 12 '22 23:10

Allen


I had the same issue, the problem was I was expecting an OnActivityResult, but when you press the back button, this was not triggered.

The solution was implementing on onResume the following code, to tell the callback the answer was empty and being able to reuse it:

@Override
protected void onResume() {
    super.onResume();
    if (fPathCallback == null)
        return;

    fPathCallback.onReceiveValue(new Uri[]{});
    fPathCallback = null;
}
like image 23
Daniel Silva Avatar answered Oct 12 '22 23:10

Daniel Silva