How to make android webview support image or files upload
Article last updated on : June 29, 2021
The Android Webview doesn’t support file upload readily, We need additional codes to make the WebView support file upload. In this article, you can learn how to make your android webview app to handle html file input or file upload support. If this is hard for you, you can contact me and i will do it for you.
If you want a great android webview source code that has already functions for file upload, download, notifications and to easily create your android webview app. check out this page
The code is a bit Complex , so you need carefully implement it to your webview activity
Note: Starting from android Marshmallow, we need ask user permission to access storage and camera for uploading and capturing images.
The code for enabling file upload in webview
First, add these code above the OnCreate method of your webview Activity
private static final int REQUEST_STORAGE = 1; private static final int REQUEST_LOCATION = 2; public ValueCallback<Uri> mUploadMessage; public static final int FILECHOOSER_RESULTCODE = 5173;
Then add this code in webChromeClient
public void openFileChooser(ValueCallback<Uri> uploadMsg) { this.openFileChooser(uploadMsg, "*/*"); } public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) { this.openFileChooser(uploadMsg, acceptType, null); } public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) { mUploadMessage = uploadMsg; Intent i = new Intent(Intent.ACTION_GET_CONTENT); i.addCategory(Intent.CATEGORY_OPENABLE); i.setType("*/*"); WebActivityDark.this.startActivityForResult(Intent.createChooser(i, "File Browser"), FILECHOOSER_RESULTCODE); } public boolean onShowFileChooser( WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) { if (mUMA != null) { mUMA.onReceiveValue(null); } mUMA = filePathCallback; Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); if (takePictureIntent.resolveActivity(WebActivityDark.this.getPackageManager()) != null) { File photoFile = null; try { photoFile = createImageFile(); takePictureIntent.putExtra("PhotoPath", mCM); } catch (IOException ex) { Log.e(TAG, "Image file creation failed", ex); } if (photoFile != null) { mCM = "file:" + photoFile.getAbsolutePath(); takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile)); } else { takePictureIntent = null; } } Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT); contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE); contentSelectionIntent.setType("*/*"); Intent[] intentArray; if (takePictureIntent != null) { intentArray = new Intent[]{takePictureIntent}; } else { intentArray = new Intent[0]; } Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER); chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent); chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser"); chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray); startActivityForResult(chooserIntent, FCR); return true; } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent intent) { super.onActivityResult(requestCode, resultCode, intent); if (Build.VERSION.SDK_INT >= 21) { Uri[] results = null; //Check if response is positive if (resultCode == Activity.RESULT_OK) { if (requestCode == FCR) { if (null == mUMA) { return; } if (intent == null) { //Capture Photo if no image available if (mCM != null) { results = new Uri[]{Uri.parse(mCM)}; } } else { String dataString = intent.getDataString(); if (dataString != null) { results = new Uri[]{Uri.parse(dataString)}; } } } } mUMA.onReceiveValue(results); mUMA = null; } else { if (requestCode == FCR) { if (null == mUM) return; Uri result = intent == null || resultCode != RESULT_OK ? null : intent.getData(); mUM.onReceiveValue(result); mUM = null; } } } private class Callback extends WebViewClient { @Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { Toast.makeText(getApplicationContext(), "Failed loading! try reloading", Toast.LENGTH_SHORT).show(); webl.loadUrl("file:///android_asset/help.html"); } } // Create an image file private File createImageFile() throws IOException { @SuppressLint("SimpleDateFormat") String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); String imageFileName = "img_" + timeStamp + "_"; File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); return File.createTempFile(imageFileName, ".jpg", storageDir); }
This is it, if you place the code in your activity the right way, your app should now support file upload
If you want me fix your webview file
Update: I see many people are having trouble to implement this code in their Android project, this is because the code is a bit long and can be complex if you are new to development.
If you don’t want use source code i mentioned above, and you can’t put this code in your app to enable webview file upload, i will do it for you for a small fee of $5. You can pay through PayPal, PayTM, GooglePay, PhonePe etc.
Just send me your webview activity file, or i can do it online through TeamViewer, AnyDesk etc.
Contact Now
For more details, contact me in the contact details above.
Starting from android Marshmallow, we need ask user permission to access storage and camera for uploading and capturing images.
So i highly recommend you use the new and advanced Android Webview Advanced Plus Source Code we developed to easily convert any website to android app. No coding required, just set your website link, app color, icon etc and the app will be ready!, and it supports upload, download, loading progress bars, notification, night mode etc. To learn more about the Android Advanced Webview Source Code and to download it, head over to this page Download Android Webview Source Code
Meet Advanced Plus
I highly recommend you use the new and advanced Android Webview Advanced Plus Source Code we developed to easily convert any website to android app. No coding required, just set your website link, app color, icon etc and the app will be ready!, and it supports upload, download, loading progress bars, notification, night mode etc. To learn more about the Android Advanced Webview Source Code and to download it, head over to this page Download Android Webview Source Code
Bro i will send you my source code could you please enable the file upload please
Hi, sure
No luck at all trying to implement this. Most of it highlights in red…
Hi kris, yes it can be hard for beginners, If you want me fix it for you, just email me your activity file thanks
Can you please provide the entire ( all files )soure code for WebView which supports file upload ??
Ok i will mail u, just respond with your email
hey bro can you share with me this project source code? my e-mail address durukan17@hotmail.com. ı will wait for the mail.
Thanks
hi can you help me out on trying to do file upload? i tried this but it didnt work. thanks
I i will try to help..let me know where you face error
Hi Admin, This code didn’t work for me,
My website is a WordPress website (Hope that’s not a criteria for it not to work.)
Can you please help me out?
Did you import all parameters?
Hi Admin, thanks for your feedback.
I don’t really know how to import parameters (I’m new to Android Studio).
I just copied the code below the existing code on “MainActivity”
Try testing the code by going to another non WordPress site, kenny
Hi, looking for a simple example to only use with android file upload.
Open website with loadUrl() and click there “Choose file….”, but can not get it to work
with webView :-/
Can you help and provide very basic working example project for this?
Yes, i will work on it soon, keep visiting for updates
Sir, I need the source code of this webview…please send me to rohithkannan.m@gmail.com.
This code work for me, cool ! thx
Im glad!
This guy really helped me out. You guys better ask him
he had good logical thinking and a problem solver for sure
flashteq@gmail.com Can you please send full working example please
Hi just email us with your activity file that need to be fixed, and i will fix your code
Really glad for him. He only helped me to resolve this issue . Thanks a lot.
Welcome naveen, it was a pleasure to work for you 🙂
Really good work. This guy is a genius. Well worth the time and money.
Thanks mate
Thanks it worked.
Yes, just email
Hi, I’m trying to implement the code but it shows me that some parameters are related to WebChromeClient and not to WebViewClient like FileChooserParams, plus cannot resolve uMma, also, does not recognize WebActivyDark. Can you help me with this?
Hi sure, we do fix code , please read the post fully
Show and Hide Toolbar ActionBar on WebView Scroll in Android Java
Hi bro, see this link
Hi is there any updates regarding mobile application if yes please contact me info@bitzsolutions.com