Categories

Follow Us @web_learners

Sunday, April 5, 2020

How to Enable File Upload in Android Webview App | Webview App Development Series Part 11

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. 

Before we move to the tutorial about uploading part I would like to let you know that we already have a post about "How to enable download in webview?". However Let's Start.



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


Code:


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:



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:



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


Code:



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

Code:




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:


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:



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.

90 comments:

  1. i am not able to upload images directly from camera.Documents folder is working properly.

    ReplyDelete
    Replies
    1. Please follow the videos properly and slowly. I have checked again and again its working. you can try by changing emulator devices. Thank you for your comment.

      Delete
    2. same here, not working upload directly from the camera. I tried on my physical device, not work either.

      Delete
    3. This not work Android version 10 and 11

      Delete
  2. Thanks so much. You're a live saver

    ReplyDelete
  3. Thanks so much. You're a life saver. Had issues with placement of some codes but I figured it out. This works, thanks again.

    ReplyDelete
  4. Camera is not getting initiated

    ReplyDelete
    Replies
    1. Please follow the videos properly and slowly. I have checked again and again its working. you can try by changing emulator devices. Thank you for your comment.

      Delete
    2. if camera does not work check if you have this:


      if you have it YOU NEED TO REMOVE IT.. then it will work (its stupid I know)

      Delete
  5. camera is not getting initiated . I am using Lenovo k50 android 6.0

    ReplyDelete
    Replies
    1. Please follow the videos properly and slowly. I have checked again and again its working. you can try by changing emulator devices. Thank you for your comment.

      Delete
  6. camera is not getting initiated. I am using Lenovo k50 android 6.0

    ReplyDelete
    Replies
    1. Please follow the videos properly and slowly. I have checked again and again its working. you can try by changing emulator devices. Thank you for your comment.

      Delete
  7. How to Enable File Upload and location capture(longitude and latitude) in Android Webview App | Webview App Development Series Part 11

    ReplyDelete
    Replies
    1. Ok I will try to create video tutorial on it.

      Delete
    2. https://www.youtube.com/watch?v=Nt0g2o8aklA&list=PLCK6ZjZwBKWGvyXnEUoC8MZ4xRKEUHKL1&index=21&ab_channel=TricksSoftware

      Delete
  8. why can't open camera..? even though add uses permission camera

    ReplyDelete
    Replies
    1. Please follow the videos properly and slowly. I have checked again and again its working. you can try by changing emulator devices. Thank you for your comment.

      Delete
  9. You are the best . It works exactly with the changes you previously mentioned.

    THANK YOU!!

    ReplyDelete
  10. You are the best . It works exactly with the changes you previously mentioned.

    THANK YOU!!!!!

    ReplyDelete
  11. Hi there, tried this code and gallery upload works perfect, but the camera upload does not. the camera app opens, i shoot a picture, click okay, and it doesnt upload. Any ideas or recommendations?

    ReplyDelete
    Replies
    1. Please follow the videos properly and slowly. I have checked again and again its working. you can try by changing emulator devices. Thank you for your comment.

      Delete
    2. You may not be requesting for Camera permission. So go to the app setting and click permission and enable camera. It should work.

      Delete
    3. Enable Permission for Camera in app settings.

      Delete
    4. Are you targeting Android API 29?
      Since Environment.getExternalStoragePublicDirectory() were deprecated in Android API 29, I suggest you to replace
      File imageStorageDir = new File(Environment.getExternalFilesDir(Environment.DIRECTORY_PICTURES), "DirectoryNameHere");
      with
      File imageStorageDir = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES),"yourPictFolderName");

      Delete
  12. Thanks so much but my own not working still

    ReplyDelete
    Replies
    1. Please follow the videos properly and slowly. I have checked again and again its working. you can try by changing emulator devices. Thank you for your comment.

      Delete
  13. Thank you. Nice tutorial, it solved my issue.

    ReplyDelete
    Replies
    1. I have tried everything but still the red mark appears, what could be a problem?

      Delete
    2. I tried all of the codes above but still I see the red mark om createImageFile(), and uploadMsg, what is a problem with that

      Delete
  14. Its working and not working,
    Its working among others to show file chooser, but it's not working , the result from camera return nothing
    I user galaxy m20 android 10
    Is it createImageFile function failed ?

    ReplyDelete
  15. Hi madam,  

         Your the best, Your Tutorials and Code Providing a good platform for people like us, stay blessed. I wanna mention one thing in few comments they mentioned the camera issue the problem is that "The upload wasn't working at all after Google WebView update" Present I'm trying to find code to resolve the issue and madam if you can find the solution means it would be helpful.  

    Thank You and Best Regards!

    ReplyDelete
  16. Can I upload any file type? like pdf, mp4 and so on?

    ReplyDelete
  17. I want a code only for file upload in webveiw not from camera. File may be pdf, doc etc. Please make a video for this, there is not enough and good video for this on youtube.

    ReplyDelete
  18. everything is working fine, but image from camera is saving, and when i click on pdf file link or gdrive link in webview, no response from app

    ReplyDelete
  19. The video was very good, it helped me a lot. Thank you. My web application loads pdf in the same option. you have a similar video on how to upload the pdf?.

    ReplyDelete
    Replies
    1. I need it too. If you have figured out something then please let me know.

      Delete
  20. Assalamualaikum. it's working. thank you very much. but I have any little problem just little problem, when I take picture from my camera, I don't see file name on my file choosen web. so my picture can't be upload because is empty. can you help it?

    but if I choose file on my file, it's working and I can upload very well. Thanks

    ReplyDelete
  21. Thank you soo much for this amazing tutorial... Will you please make a video on how to upload video file...

    ReplyDelete
  22. hi thanks first of all for making this great tutorial series i have the same issue gallery work fine but camera uploads not working i copy and paste code as per video directions and do it 4th times also allow permissions to camera but issue is there please guide the solution thanks

    ReplyDelete
    Replies
    1. Follow my this video, your problem is solved in this video. Video Link: https://youtu.be/Nt0g2o8aklA

      Delete
  23. camera opens and after taking picture its not do anything on press ok

    ReplyDelete
  24. Hi, a code like this worked since android 10. When the app runs on android 10 onShowFileChooser is not called at all.
    Do you have any idea or a workaround ?

    ReplyDelete
  25. Hi, this works before android 10. If the app is running on android 10 the method "onShowFileChooser" is not called at all.
    Do you know any workaround ?

    Thanks

    ReplyDelete
  26. Thank you, this is nice tutorial, it's worked!

    ReplyDelete
  27. Hi Bro. May I know how to enable customer current location

    ReplyDelete
  28. I don't no your language but i understand. I really hard to search, time god. keep it up and great job thanks

    ReplyDelete
  29. Hi, my website makes call through browser and connects to whats-app, how can I activate both through webview ?

    ReplyDelete
  30. How can use code for upload with download support. Here you guided for upload file only and for download in previous tutorial. When I used code for download and after that used the above codes for upload, there are errors of some code already used.

    ReplyDelete
  31. when i click on choose file, and after click cancel and once again i try to click on choose file it doesnot open
    please help me out

    ReplyDelete
    Replies
    1. After your complain, I have checked multiple times , but I saw it's working perfectly, please check your code again.

      Delete
  32. what if we want to upload other format such as pdf, documents ?

    image upload is working fine but i cannot upload any other files

    ReplyDelete
  33. How to Maintain PHP Login Logout Session ? Please Help me

    ReplyDelete
  34. Hi, it worked but I can't select more that one file. I can only pick one image file at a time yet I really want to be able to select multiple files at a time. Any help will really be helpful

    ReplyDelete
  35. after capturing with camera file not selecting in input this is only in android 11 please can u check once and tell me

    ReplyDelete
    Replies
    1. If this method is not working for you, then you can follow my another method. Here is the link: https://weblearners.blogspot.com/2021/06/how-to-enable-video-image-and-file-upload-in-android-webview-app.html

      Delete
  36. still says

    error: cannot find symbol
    private ValueCallback mUploadMessage;
    ^
    symbol: class uri
    location: class MainActivity

    ReplyDelete
  37. Thanks a lot for this post,
    Can I know how to remove camera option from this?
    Bcz I only need document choosing method.
    Any way this post was very very helpful to me so Thank you again 🤝🤍🤍🤍

    ReplyDelete
  38. In my case uploading is working fine but in case i call this function along with onCreateWindow, onCreateWindow method is working fine but file uploading option does not working, kindly help me.

    ReplyDelete