How to Enable File Upload in Android Webview App | Webview App Development Website to app Convert Part 11

 

 

by TS Durjoy

How to Enable File Upload in Android Webview App? That's what most of the people search in the google and youtube. Because this is the most complicated part of the webview app development.

 

Are you stuck with the problem "How to allow file upload and download in webview" also? Don't worry, Today I am going to show you how to write the code in android studio to support file upload in android webview app. Let's Start.

 

 

 

First of all, You need to give 3 permission in AndoidManifest.xml. Those are given below:

Code:

Top of Form

Bottom of Form

After that, you need to write code in MainActivity.java file. You need to take some global variable for opening file chooser with camera option, see below code:

Code:

Top of Form



Bottom of Form

After that, you need to write code for showing "file chooser". Just copy the below code and paste it into your WebChromeClient function. By the way, you can see below code contain "web" is my variable name, I have retrieved and stored my Webview from activity_main.xml.you need to replace that variable "web" with your variable name.

Code:

Top of Form

Bottom of Form


Then you need to write Override function after "onCreate", See below code.

Code:

Top of Form

Bottom of Form


Don't forget to write this line of code in your "strings.xml" file.

Code:

Top of Form

Bottom of Form





Whenever you will paste these codes in your project you see lots of red words. you need to click on that red word then press "alt+Enter" then it will import class automatically. Or you can just copy below import class and paste in your import section. all the Error will go.

Code:

Top of Form

Bottom of Form

After paste above import class, you will see almost all the error gone, only 5 error still there, they are "uri", "uploadMsg" and "result". you need to click on "result" then will show suggestion you need to click on that, then all the error will go. See below picture:





So, the whole final code will be in MainActivty.java is:

Code:

Top of Form

Bottom of Form


In conclusion, we have finished our coding for our Webview App. Now we will run our project on Emulator. As you can see in my screenshot, it's working perfectly.





By the way, If you face any problem, let us know in the comment box below. We will try our best to solve your problem. Thanks.

 

6 comments:

  1. I would like to get the github of the projet

    ReplyDelete
  2. It works fine until I choose a file to upload: the interface close and the app goes back to the main page instead of the page where I started to upload.

    ReplyDelete
  3. Funcionou perfeitamente no android 10, alguém que use o android mais avançado 12 ou 13 me diga se funcionou para voce tambem?

    ReplyDelete

Powered by Blogger.