24 Feb 2021

How To Enable File Upload in WebView?


If you are trying to enable we view file upload feature. Then this is the post for you. This post is about How to enable File Upload in Web view?

How to enable file upload in WebView?

To enable webview file upload in your application. Follow the steps.

Creating Global Scope Variable

Create a global object variable.

  ValueCallback<Uri[]> vc;
  

Override onFileChooser

WebViewChormeClient abstract class's FileUpload method have to Overide to get file upload feature under webview.

Java
WebChromeClient wcc = new WebChromeClient(){

@Override
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams)

{

Intent fileChooserIntent = fileChooserParams.createIntent();

startActivityForResult( fileChooserIntent, chooserRequestCode);

vc = filePathCallback;

return true;

 }

}

Set to webview, By calling WebView.setWebChromeCloent(wcc);

Overide onActivityResult.

Need to Overide an Activity's onActivityResult() method. Because in WebView WebChromeClient has been calling file chooser activity. so, Here we are going to take data uri of selected files.

Java
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		
		
     <Uri> selectedFileUri = WebChromeClient.FileChooserParams.parseResult(resultCode, data);
	
	
		
	vc.onReceiveValue( selectedFileUri);

	}
    
    

Reaction:

No comments:

No Comments

COMMENT FROM MESSAGE