Categories

Follow Us @web_learners

Sunday, June 27, 2021

How to Enable Video, Image and File Upload in Android Webview App | Webview App Development Series Part 16

June 27, 2021 8 Comments

 Hi, Everyone! I have got many requests and comment about "File upload not working in android webview app", also "can not uploading pdf or video file in android webview app", you all have requested me to help you for solving the problem.


Today I will solve all the problems about "How to enable or support file uploading in android webview app", "How to allow any type file uploading such as pdf, ppt, audio, video, image, txt"? anything you will be able to upload in your webview android apps. Just follow my tutorial from beginning to end. In the previous video, I have showed you How to Enable Download in Android Webview App.

So, Let's start, First of all, you need to add 3 permission for "access camera, write in external storage and Internet" in your AndroidManifest.xml file. 


Code:


After adding the permission, now need to move to the "MainActivity.java" file, now let's take 5 variable, which are: 

Code:


Let's demonstrate, why we have taken these variable,  we have taken a private static string file called "file_type", this is for storing file type we want to upload in our webview app, such as pdf, ppt, pptx, ppt, audio, video, image, doc, zip, RTF, txt, etc. 


As we want to allow all types of these files uploading in our android webview app, so we have defined file_type = "*/*". "*/*" this means it will allow all types of file uploading in the android webview app.


cam_file_data: This String variable "cam_file_data"  for storing camera file information.

ValueCallback<Uri> file_data: We have taken the "ValueCallback" variable, we have named it "file_data", this will store data after selecting the file.

ValueCallback<Uri[]> file_path: Another "ValueCallback" variable, we named it file_path, this will store file location.


Now let's move to the "onCreate" method, and will write a function method called onShowFileChooser in our setWebChromeClient, and all the necessary code will write on this function,  See the  Below Code:


Code:


Now we will write a function "onActivityResult"  for selecting the file from the Camera or SD card in android webview app.  This function will be outside of "onCreate" method. See the  Below Code:


Code:


Now we will write function called "onConfigurationChanged"

Code:


Now we will write a function "file_permission" for checking and asking for required file permissions.


Code:


We have taken a variable "photoFile"  in the "onActivityResult" function, there we have stored a function called "create_image()". Now we will create that function. 


Code:


Now we will write function for video file

Code:


We have finished the coding. Now Let's run our project and check whether any type of file is uploading in our android webview app or not.


    

Figure: How to enable file upload in webview android

As you can see from the above image, I have successfully uploaded an audio MP3 file, MP4 video file, and image. This way you will able to do android webview upload files, upload all types of files, pdf, ppt, audio, video, image, txt etc.


In the next tutorial, I will show you How to enable multiple file upload in the Android webview app.